DataGridView - loop through selected rows, change value of sorted coloumn.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cday119
    New Member
    • Mar 2008
    • 29

    DataGridView - loop through selected rows, change value of sorted coloumn.

    Hi Everyone,

    So I have a datagridview and it has a column called status. It has 3 values: New, Printed, and Shipped.

    Now a user can select multiple rows and click "set to printed". I then do a foreach loop on datagridview.se lectedrows and change the status to printed. Now the problem is that if the datagridview is sorted by status, when the status is changed from new to printed, that row is moved from being under new to printed and all the code after is executed on the next row. Here is what the code looks like.

    Code:
     For Each row As System.Windows.Forms.DataGridViewRow In frmMain.OrderDataGridView.selectedRows
                row.Cells("status").Value = "Printed"
                row.Cells("test").Value = "test"
            Next
    So, like I said before, if the datagridview is sorted by the status cloumn, and 2 rows are selected. Then first row will hit the first line of code, execute it and change postion. So the 2nd row becomes the 1st row and row("status") is not changed. Any ideas on how to resolve this?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    A.
    Copy the data out of the grid so you don't have to fight the auto sort.
    Do your calculations.
    Put the data back into the grid.

    B.
    Don't do a Foreach loop.
    Use a conventional 'for' loop with an int you can use for the index. That way when item 27 becomes item 9 you don't care; you still move on to item 28.

    Comment

    • cday119
      New Member
      • Mar 2008
      • 29

      #3
      What I ended up doing was saving the dgv to the database, did a sql query on to update the status in the database, then refreshed the datagridview. Thanks for your help man!

      Comment

      Working...