How to delete row from gridview's RowDataBound event

I am trying to delete a row from a gridview (based on a condition) and then add than row to another gridview inside of the "master" gridview's RowDataBound event. Originally I did not know that in order to call .DeleteRow(i) you needed to have an "ondelete" event handler. However, since all the gridview's .DeleteRow method does is call this event handler, I am confused as to how to use it. Can someone please help point me in the right direction?

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

        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

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

        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


The best way to do this is to manipulate this at the datasource level rather than manipulating the UI components themselves.

For example:

    DataTable one  = ...
    DataTable two = ...

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

   for(var item in removeRows)
   //now delete from initial table
   foreach(var item in removeRows)

   //Now bind both grids


Update - Sample toy program in VB.NET Hopefully you can adapt it to your situation.

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}

    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}
    Dim removeRows = (From c In one.AsEnumerable() Where c.Field(Of Integer)("one") = 5).ToList()

    For Each item As DataRow In removeRows

    For Each item As DataRow In removeRows

End Sub

I just realized that you are using VB.NET. You should be able to translate the above from C# to VB.NET. The general idea is there, anyway.

Need Your Help

Change GridView's “Checked Out” column from a button to a label based on whether cardNumber is null or not

c# sql visual-studio

NOTE: I'm a beginner web developer, somewhat familiar with PHP. I've been curious about ASP.NET and wanted to give it a go.

Schema Spy unable to find postgresql driver

java postgresql jar driver schemaspy

I want to use Schema Spy to generate schema diagrams,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.