2012-09-07 89 views
0

我想從gridview中刪除一行(基於條件),然後將行添加到另一個gridview內的「master」gridview的RowDataBound事件中。最初我不知道,爲了調用.DeleteRow(i)你需要有一個「ondelete」事件處理程序。但是,由於所有gridview的.DeleteRow方法都是調用這個事件處理函數,所以我很困惑如何使用它。有人能幫助我指出正確的方向嗎?如何從gridview的RowDataBound事件中刪除行

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

除了'DeleteRow'方法外,您還可以簡單地從主網格的數據源中刪除該行,並在您選擇的任何事件(例如CommandEvent或Button-Click)中將其添加到嵌套網格的源。之後您只需調用'MainGrid.DataBind()'。 –

+0

我有一個獨立於RowDataBound事件的公共子項中聲明的數據源。爲了做到這一點,我需要更改數據源的範圍嗎?不幸的是我需要使用RowDataBound事件來測試條件。 – NealR

回答

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。無論如何,總體思路就在那裏。

+0

ImportRow方法是否從原始DataTable中刪除該行?從我在這裏看到的:http://msdn.microsoft.com/en-us/library/system.data.datatable.importrow(v=vs.80).aspx它只是一個副本。 – NealR

+0

@NealR它沒有。你需要自己做。我用完整的工作代碼更新了我的答案。 – Icarus

+0

我有一個不可能的時間轉換從...在哪裏代碼到VB。你知道這有可能嗎?我得到錯誤oc C.Field和分別。 c.Field說:「重載解析失敗,因爲沒有可訪問的'Field'接受這個數量的參數。」 說「'DateTime'是一種類型,不能用作表達式」 – NealR

相關問題