2012-12-12 3 views
0

조건을 만족하면 GridView에서 전체 행을 제거하려고합니다.행이 조건을 만족할 때 gridview에서 전체 행 제거

는 여기에서 나는 GridView 채워되어있는

a Description Column from Product table in database

을 가지고있다.

sqlcommand을 내 vb 앱에만 select the Description which does not contain the String s으로 설정합니다.

여기 String s has two keywords "Tomatoes" and "Apple". 따라서 Sql 쿼리는 Description column that does not have "Tomatoes" and "Apple"을 검색해야합니다.

따라서 조건을 만족하는 행을 제거하여 Gridview을 업데이트해야합니다.

나는 "토마토"와 "애플"을 가지고

Description row in GridView을 제거하는 어려움에 직면하고있다. 결과로 다른 GridView를 채우려고했지만 웹 페이지에서 일부 중단 점을 지정한 값을 보았 기 때문에 모든 것이 정확하다는 사실에도 불구하고 나타나지 않습니다. 어떤 제안이나 생각?

Dim cmd1 = New SqlCommand(" SELECT DISTINCT [Description] FROM [Product] WHERE ([Description] LIKE '%' + @Description + '%')", conn) 
         cmd1.Parameters.AddWithValue("@Description", s) 

     MsgBox("NO") 

     For i As Integer = 0 To GridView1.Rows.Count - 1 
     DA.SelectCommand = cmd1 
     DA.Fill(dt) 

     'row is of a GridViewRow datatype As GridView1.Rows 
      row.Cells.RemoveAt(i) '' do not know if it is correct or not 

     'GridView3.DataSource = '' dt tried with no luck 
     'GridView3.DataBind() '' tried with no luck 
      cmd1.Dispose() 
      DA.Dispose() 
      dt.Clear() 
      dt.Dispose() 
     Next 
     End If 
+3

보다 정확한 구현은 이미로드 된 GridView에서 데이터를 제거하는 대신 GridView의 데이터 소스에서 행을 제거한 다음 DataBind()를 호출하는 것입니다. thread [here] (http://stackoverflow.com/questions/592106/how-to-deleterowrow-from-gridview)를 참조하십시오. –

+0

@SandraWalters 그래서 어떻게 sqlDataSource에서 오리지널'Description'을 지우고 싶지는 않지만 GridView에서만 행을 삭제하려고 할 수 있습니다 – HShbib

답변

1

당신은 여전히 ​​데이터 소스를 변경할 수, 원본을 조작하지 않고 :

여기 내 코드입니다. "dt"변수를 데이터베이스의 데이터로 채 웁니다.

var stuffIActuallyWantInMyGrid = new List<DataSet>(); //A list of whatever you dt is of 
    foreach(var x in dt) 
    { 
     if(x == WhateverMyCriteriaAre) 
     { 
      stuffIActuallyWantInMyGrid.Add(x); 
     } 
    } 
    GridView3.DataSource = stuffIActuallyWantInMyGrid; 
    GridView3.DataBind(); 

같은 (그래, 난이 C# 코드 알고,하지만 당신은 절대적으로 의 GridView의에서 데이터를 유지해야하는 경우 다른 사람이 C# ;-)

1

으로이 질문을 태그와 그것을 통해 다음 루프 기본 데이터 소스에 표시되지만 표시하지 않으려면 GridView에서 RowDataBound 이벤트를 처리하는 것이 좋습니다. 이 메소드 내에서 기준에 따라 주어진 행을 테스트하십시오. 행이 일치하면 Visible 속성을 False로 설정합니다.

Public Sub MyGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 

    If e.Row.RowType = DataControlRowType.DataRow Then 
     If e.Row.Cells(0).Text = "((value of row to hide))" Then 
      e.Row.Visible = False 
     Else 
      e.Row.Visible = True 
     End If 
    End If 

End Sub 
관련 문제