2012-12-12 76 views
0

我想,如果它满足的条件从GridView删除完整的行。从一个GridView删除完整的行当行满足条件

在这里我有

a Description Column from Product table in database与一个GridView填充。

我在VB应用程序中设置的sqlcommandselect the Description which does not contain the String s

这里String s has two keywords "Tomatoes" and "Apple"。所以Sql Query应该检索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的数据源中删除行,然后调用的DataBind(),而不是它已经被加载之后从GridView的数据。看到线程[这里](http://stackoverflow.com/questions/592106/how-to-delete-row-from-gridview)。 –

+0

@SandraWalters所以我怎么才能实现只从GridView中删除行,因为我不想从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#代码,但有人标记这个问题为C#;-)

1

如果你绝对必须保留GridView的数据基础数据源,但不希望显示,一种解决方案是处理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