我想從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
除了'DeleteRow'方法外,您還可以簡單地從主網格的數據源中刪除該行,並在您選擇的任何事件(例如CommandEvent或Button-Click)中將其添加到嵌套網格的源。之後您只需調用'MainGrid.DataBind()'。 –
我有一個獨立於RowDataBound事件的公共子項中聲明的數據源。爲了做到這一點,我需要更改數據源的範圍嗎?不幸的是我需要使用RowDataBound事件來測試條件。 – NealR