0

我正在尝试从 gridview 中删除一行(基于条件),然后将行添加到“master”gridview 的 RowDataBound 事件内的另一个 gridview 中。最初我不知道为了调用 .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
4

1 回答 1

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。无论如何,总体思路就在那里。

于 2012-09-07T14:29:20.710 回答