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

    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

Answers


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

For example:

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();

}

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}
        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

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# asp.net 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.