Results 1 to 6 of 6

Thread: [RESOLVED] Changing Data in Selected Rows

  1. #1

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Resolved [RESOLVED] Changing Data in Selected Rows

    This has turned out to be more complex than expected. I'm throwing it out there in case anyone has had to deal with a similar situation in the past.

    The short version: I need to loop through the selected rows of a bound datagridview, and make similar changes to each record, when some of the cells being changed may also be part of the SortedColumn.

    The long version: I've been trying to use a foreach loop on the selected rows, but the problem is the sorting: if one of the column's due to change is the column the list is being sorted by, then data ends up everywhere: I believe my selection is lost the moment it is resorted.

    I've tried several solutions, including manually sorting them (I lose the selection), dropping the selected rows into a List and re-selecting them after the sort (was told that the row's were not part of the DataGridView), and even turning off sorting (which works, but makes the application impossibly hard to use).

    Any help? All I need to do is apply the same changes to all the currently selected rows.

    Thanks,
    Qu.
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

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

    Re: Changing Data in Selected Rows

    When you bind a DataTable to a control it is actually the contents of the DefaultView, which is a DataView, that gets displayed. That's how the grid can be filtered and sorted without the DataTable changing. It's the DefaultView that gets filtered and sorted. That means that the rows in the DefaultView are always in sync with the grid. You basically have two choices if you want to edit the data source based on the selected rows in the grid:
    CSharp Code:
    1. DataRowView view;
    2.  
    3. foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
    4. {
    5.     view = (DataRowView)row.DataBoundItem;
    6.  
    7.     // Edit view here.
    8. }
    or:
    Code:
    DataView view = ((DataTable)this.dataGridView1.DataSource).DefaultView;
    DataRowView row;
    
    for (int index = 0; index < this.dataGridView1.RowCount; index++)
    {
        if (this.dataGridView1.Rows[index].Selected)
        {
            row = view[index];
    
            // Edit row here.
        }
    }
    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

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Re: Changing Data in Selected Rows

    Thanks jmc, after establishing out how to work with DataRowViews and incorporating them with my existing functions, that seems to work.

    Cheers!
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Changing Data in Selected Rows

    For others reading this, a DataRowView is a view of a DataRow and can be treated the same as a DataRow for most purposes. As such you can get and set a field by column index or column name, just as you can with a DataRow.
    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

  5. #5
    New Member
    Join Date
    Dec 2008
    Posts
    4

    Re: Changing Data in Selected Rows

    Hi jmcilhinney,

    I appreciate the code you've given below. I'm finding there is a problem with using the foreach loop (on DataGridView) to update rows.

    Steps to cause issue:
    - User has clicked on DGV column header to sort the rows
    - User is updating data in the same column the sort is applied
    - When to loop is running, the rows are auto re-sorting (based on the updated cell data)

    Becuase the rows are moving position, the correct rows are not always being updated correctly. I have not found a way to stop the sort.


    DataRowView view;
    foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
    {
    view = (DataRowView)row.DataBoundItem;
    // Edit view here.
    view.Row["Date"] = newDate;
    }

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Changing Data in Selected Rows

    Quote Originally Posted by myNameIsRon
    Hi jmcilhinney,

    I appreciate the code you've given below. I'm finding there is a problem with using the foreach loop (on DataGridView) to update rows.

    Steps to cause issue:
    - User has clicked on DGV column header to sort the rows
    - User is updating data in the same column the sort is applied
    - When to loop is running, the rows are auto re-sorting (based on the updated cell data)

    Becuase the rows are moving position, the correct rows are not always being updated correctly. I have not found a way to stop the sort.


    DataRowView view;
    foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
    {
    view = (DataRowView)row.DataBoundItem;
    // Edit view here.
    view.Row["Date"] = newDate;
    }
    Code:
    List<DataRowView> rows = new List<DataRowView>();
    
    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {
        rows.Add((DataRowView)row.DataBoundItem);
    }
    
    foreach (DataRowView row in rows)
    {
        row["Date"] = newDate;
    }
    Note that I edit the field in the DataRowView directly. As I said earlier, a DataRowView and a DataRow can be treated in the same way for most purposes. There's no need to get the DataRow itself to edit the data.
    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

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