Get DataRow from GridViewCollectionChangedEventArgs

1 Answer 405 Views
General Discussions GridView
Phong
Top achievements
Rank 1
Phong asked on 28 Sep 2021, 03:56 PM

I'm using RadGridView for WinForms.  How do I get the DataRow object from the RowsChanged event below?

private void radGridView2_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)
 {
     if (e.Action != NotifyCollectionChangedAction.Reset)
     {
                DataRow myRow = ????
     }
}

Thanks!

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 30 Sep 2021, 11:26 AM

Hello Phong,

It looks like you are having the grid data bound to a data table. In that case, the data-bound item of each row would the DataRowView object. It can be accessed this way: 

GridViewRowInfo currentRow = this.radGridView1.CurrentRow;
DataRowView dataRowView = currentRow.DataBoundItem as DataRowView;

Now about the RowsChanged event, the event arguments have information about the changed rows exposed by the NewItems property. You can iterate those items and cast their data-bound items to DataRowView. Then from the DataRowView you can extract the actual DataRow object: 

private void RadGridView1_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)
{
    if (e.NewItems == null)
    {
        return;
    }

    foreach (var item in e.NewItems)
    {
        GridViewRowInfo row = item as GridViewRowInfo;
        if (row == null)
        {
            continue;
        }

        var rowView = row.DataBoundItem as DataRowView;
    }
}

I hope this will help.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
General Discussions GridView
Asked by
Phong
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or