PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Converting a DataGridViewRow to a DataRow?-VBForums
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
    4,865

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,585

    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)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    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
    3,845

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,585

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    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
  •  



Featured


Click Here to Expand Forum to Full Width