如何从gridview RowDataBound事件中删除行

时间:2012-09-07 14:05:54

标签: asp.net vb.net gridview

我正在尝试从gridview中删除一行(基于条件),然后将行添加到" master"中的另一个gridview中。 gridview的RowDataBound事件。最初我不知道为了打电话.DeleteRow(i)你需要有一个" ondelete"事件处理程序但是,由于所有gridview的.DeleteRow方法都会调用此事件处理程序,因此我对如何使用它感到困惑。请有人帮我指点正确的方向吗?

Protected Sub grdProduct_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdProduct.RowDataBound

    ' Grey out expired products
    Dim row As GridViewRow
    row = e.Row

    Dim incomingDate As String
    Dim incomingStatus As String = ""

    incomingDate = row.Cells(3).Text.ToString()
    incomingStatus = row.Cells(5).Text.ToString()

    If (e.Row.RowType <> DataControlRowType.DataRow) Then
        Exit Sub
    End If

    Try
        Dim expDate As Date = incomingDate
        If (expDate < DateTime.Today Or incomingStatus.Equals("D")) Then

            'Create object for RowValues
            Dim RowValues As Object() = {"", "", "", "", "", ""}

            'Create counter to prevent out of bounds exception
            Dim i As Integer = row.Cells.Count

            'Fill row values appropriately
            For index As Integer = 0 To i - 1
                RowValues(index) = row.Cells(index).Text
            Next

            'create new data row
            dProdRow = dProdtable.Rows.Add(RowValues)
            dProdtable.AcceptChanges()

            grdProduct.DeleteRow(e.Row.RowIndex)
        End If
    Catch ex As Exception
    End Try
End Sub

Protected Sub grdProduct_Delete(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles grdProduct.RowDeleting
    'Not sure what to do here
End Sub

1 个答案:

答案 0 :(得分:0)

执行此操作的最佳方法是在数据源级别操作此操作,而不是操纵UI组件本身。

例如:

if(!IsPostback)
{
    DataTable one  = ...
    DataTable two = ...

    var removeRows = (from c in one.AsEnumerable()
                     where c.Field<DateTime>("incomingDate")< incomingDate || 
                           c.Field<string>("incomingStatus")=="D"
                     select c).ToList();

   for(var item in removeRows)
   {
       two.ImportRow(item);
   }
   //now delete from initial table
   foreach(var item in removeRows)
   {
       one.Rows.Remove(item);
   } 

   //Now bind both grids
   grid1.DataSource=one;
   grid1.DataBind();
   grid2.DataSource=two;
   grid2.DataBind();

}

更新 - VB.NET中的玩具程序示例希望您可以根据自己的情况进行调整。

Sub Main
    Dim one As New DataTable()
    one.Columns.Add("one", GetType(Integer))
    For i As Integer = 0 To 9
        Dim r As DataRow = one.NewRow()
        r.ItemArray = New Object() {i}
        one.Rows.Add(r)
    Next

    Dim two As New DataTable()
    two.Columns.Add("one", GetType(Integer))
    For i As Integer = 0 To 9
        Dim r As DataRow = two.NewRow()
        r.ItemArray = New Object() {i}
        two.Rows.Add(r)
    Next
    Dim removeRows = (From c In one.AsEnumerable() Where c.Field(Of Integer)("one") = 5).ToList()

    For Each item As DataRow In removeRows
        two.ImportRow(item)
    Next

    For Each item As DataRow In removeRows
        one.Rows.Remove(item)
    Next

End Sub

我刚才意识到你正在使用VB.NET。您应该能够将上述内容从C#转换为VB.NET。无论如何,总的想法就在那里。