Results 1 to 6 of 6

Thread: Converting a DataGridViewRow to a DataRow?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    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,
    Blake

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    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:
    1. 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:
    1. Dim row As DataRowView = DirectCast(myDataGridView.CurrentRow.DataBoundItem, DataRowView)

  3. #3
    New Member
    Join Date
    Jun 2015
    Posts
    7

    Re: Converting a DataGridViewRow to a DataRow?

    Quote Originally Posted by blakemckenna View Post
    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)

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Converting a DataGridViewRow to a DataRow?

    Quote Originally Posted by wes4dbt View Post
    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.

  6. #6
    New Member
    Join Date
    Jun 2015
    Posts
    7

    Re: Converting a DataGridViewRow to a DataRow?

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width