I have a gridview on my ASP website that needs to process each row and, based on a condition, either keep the row or remove it and add it to another gridview. I’ve figured out how to remove the row by simply using one of the built-in methods. Everything I find online is telling me to use the “gridview.Rows.Add(row)” property, however it creates the following error in Visual Studio:
“‘Add’ is not a member of ‘System.Web.UI.WebControls.GridViewRowCollection’.”
- grdTraining is the “master” gridview containing the results to be examined.
- grdExpTraining is the “secondary” gridview that takes the rows pulled from the “master.”
-
grdTraining_RowDataBound is a method that gets called everytime the website finds records to place into grdTraining.
Protected Sub grdTraining_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdTraining.RowDataBound ' Grey out expired training courses Dim row As GridViewRow row = e.Row Dim incomingDate As String incomingDate = row.Cells(4).Text.ToString() If (e.Row.RowType <> DataControlRowType.DataRow) Then Exit Sub End If Try Dim expDate As Date = incomingDate If (expDate < DateTime.Today) Then grdExpTraining.Rows.Add(row) 'The line that is causing the error grdTraining.DeleteRow(trnIndex) End If Catch ex As Exception End Try trnIndex += 1 End Sub
I was able to add a row to my grid view using the following code. I basically had to re-do the entire process again, treating the nested gridview like a completely new gridview. This also only works if you plan on placing one row in the nested gridview. You can play with the scope of the DataTable and DataRow declarations to do otherwise.