Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
DataRow dr;
dr = ((dgv.DataSource) as DataTable).Rows[dgv.CurrentRow.Index];


dgv is DataGridView predefined on winform.
dr is single always.

What I have tried:

Above code was found.
But it doesn't work right. Because 'CurrentRow.Index' is not macthed to 'DataTable's Index'.
I think to copy DataGridViewRow to DataRow is simplest way. Is it impossible?

Reason trying this is for Update to/Delete from database when dgv is changed. Any other better solution for remote DB CRUD from local dgv should be great.

Thanks in advance.
Posted
Updated 10-Oct-18 21:34pm
v3

1 solution

Use the CurrentRow item and it's DataBoundItem property: for me, it returns a DataRowView which lets me access the Row itself:
DataRowView drv = myDataGridView.CurrentRow.DataBoundItem as DataRowView;
if (drv != null)
    {
    DataRow dr = drv.Row;
    }
 
Share this answer
 
Comments
bryandkko 11-Oct-18 21:58pm    
Thank you but it's not what I want.
I just want to copy by value but kind of handle.
Values before 'RowDirty' are needed for update remote DB.
OriginalGriff 12-Oct-18 2:11am    
Do you want to try explaining that in more detail?

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900