-
Mar 22nd, 2010, 07:26 PM
#1
Thread Starter
PowerPoster
Converting a DataGridViewRow to a DataRow?
I have a DataGridView that is bound to a DataTable thru the code. I want to be able to modify the data when a row is selected from the Grid. How do I reference that row of data from the grid so I can convert it into the DataTable row to be updated? I'm trying to do something like this:
Code:
Dim row as DataGridViewRow = DirectCast(dgvAddress.CurrentRow, DataRow)
This isn't working. I'm getting an error basically stating that I can't convert a DataGridViewRow into a System.Data.DataRow.
What's is the correct way to do this?
Thanks,
-
Mar 22nd, 2010, 07:38 PM
#2
Re: Converting a DataGridViewRow to a DataRow?
There is no conversion. A DataGridViewRow is a DataGridViewRow and a DataRow is a DataRow. They are two very different things. If what you're saying is that, when you select a DataGridViewRow, you want to get the bound DataRow, that is something else and involves no conversion. That code doesn't really make sense because you're trying to get a DataGridViewRow, cast it as type DataRow, then assign it to a DataGridViewRow variable.
First up, I would suggest that you use a BindingSource. You would then do this:
vb.net Code:
Dim row As DataRowView = DirectCast(myBindingSource.Current, DataRowView)
The BindingSource exposes the bound data and the Current property exposes the current item. When you bind a DataTable, the data actually comes from its DefaultView, which is a DataView, so the items are actually DataRowView objects. If you really need to, you can get the corresponding DataRow from the Row property of the DataRowView, but it would be rare that you'd need to do that and you certainly don't just to edit data.
If you really wanted to do it without a BindingSource then the equivalent code would be:
vb.net Code:
Dim row As DataRowView = DirectCast(myDataGridView.CurrentRow.DataBoundItem, DataRowView)
-
Dec 15th, 2017, 12:18 PM
#3
New Member
Re: Converting a DataGridViewRow to a DataRow?
Originally Posted by blakemckenna
I have a DataGridView that is bound to a DataTable thru the code. I want to be able to modify the data when a row is selected from the Grid. How do I reference that row of data from the grid so I can convert it into the DataTable row to be updated? I'm trying to do something like this:
Code:
Dim row as DataGridViewRow = DirectCast(dgvAddress.CurrentRow, DataRow)
This isn't working. I'm getting an error basically stating that I can't convert a DataGridViewRow into a System.Data.DataRow.
What's is the correct way to do this?
Thanks,
Let's try something a little simpler.
DataTable1.Rows(DataGridView1.CurrentRow.Index)
-
Dec 15th, 2017, 02:16 PM
#4
Re: Converting a DataGridViewRow to a DataRow?
Timeshell,
Not sure why you dug up this old post but your suggestion is incorrect. If the datagridview has been sorted (like by clicking a column header), it will produce the wrong value.
-
Dec 15th, 2017, 11:40 PM
#5
Re: Converting a DataGridViewRow to a DataRow?
Originally Posted by wes4dbt
Timeshell,
Not sure why you dug up this old post but your suggestion is incorrect. If the datagridview has been sorted (like by clicking a column header), it will produce the wrong value.
Indeed, that suggested solution will work if there has been no sorting or filtering but, unless you can guarantee that's the case, it is definitely unsafe. As I said back when this thread was current, when you bind a DataTable, the data actually comes from the DefaultView. If you have set the Sort or RowFilter property or called an associated method of that DataView then the table row order and the grid row order won't match. Clicking a column header in the grid calls the ApplySort method of the DataView's IBindingList implementation.
-
Dec 16th, 2017, 04:45 PM
#6
New Member
Re: Converting a DataGridViewRow to a DataRow?
Originally Posted by jmcilhinney
Indeed, that suggested solution will work if there has been no sorting or filtering but, unless you can guarantee that's the case, it is definitely unsafe. As I said back when this thread was current, when you bind a DataTable, the data actually comes from the DefaultView. If you have set the Sort or RowFilter property or called an associated method of that DataView then the table row order and the grid row order won't match. Clicking a column header in the grid calls the ApplySort method of the DataView's IBindingList implementation.
Yes, I was looking at it from a very simplistic point of view. I agree with your comments.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|