473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

29
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.selectedrows 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.

Expand|Select|Wrap|Line Numbers
  1.  For Each row As System.Windows.Forms.DataGridViewRow In frmMain.OrderDataGridView.selectedRows
  2.             row.Cells("status").Value = "Printed"
  3.             row.Cells("test").Value = "test"
  4.         Next
  5.  
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?
Oct 21 '09 #1
2 11555
tlhintoq
3,525 Expert 2GB
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.
Oct 21 '09 #2
cday119
29
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!
Oct 22 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: VM | last post by:
After I do DataTable.Select() on my table, how can I display these rows in my datagrid? I cannot use a dataview because these selected rows will be altered. For example, if my query returns 1 row...
4
by: Henry J. | last post by:
Can somebody tell me how to delete multiple rows in a datagridview selected by the user? I tried the following two methods to no avail. I have a myDataGridView that is bound to myDataTable's...
1
by: Mark | last post by:
Hello, How do I loop through all selected rows of datagridview? Any help greatly appreciated! Thanks in advance
1
by: Tim Kelley | last post by:
I have a dataViewgrid in my project in which the user will select multiple rows. The will click a button and my program will perform some operation on the selected records (using the ID column...
2
by: fanoftvb | last post by:
Hi, i currently has this code. When on click on the "Approve" button, the database of all the rows will change. I have "Approve" button on every row. How do i make it so that only the selected rows...
1
by: PawelR | last post by:
Hi Group, In my application I have DataTable which is displayed in DataGridView via DataView: DataView myView = new DataView(myTable); myDataGridView.DataSource = myView; One column im...
1
by: wojjed | last post by:
Hi Here's how i fill the list : Private Sub CommandButton1_Click() Dim sFilename As Variant sFilename = Application.GetOpenFilename("Excel files (*.xls; *.csv), *.xls; *.csv", 0,...
0
by: wasim jack | last post by:
sir,I want to change value of combobox of datagridview on the basis of previous combobox value of the same raw of same datagridview
5
by: neelsfer | last post by:
I have a button on mainform that create current time.In the subform, i already have data in some of the fields. I would like to highlight these specific rows in the subform (select them with mouse)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.