473,835 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

29 New Member
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.

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 11605
tlhintoq
3,525 Recognized Expert Specialist
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 New Member
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
1193
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 and I decide to modify the data in that row (which is done through the grid), those changes should be reflected in the main table. Also, I can't go through the rows with a loop and update them manually (or I'd prefer not to) because these tables...
4
22394
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 myDataView. The column "Name" is the primary key. I want to delete the rows selected by the user: foreach (DataGridViewRow drv in myDataGridView.SelectedRows) {
1
1083
by: Mark | last post by:
Hello, How do I loop through all selected rows of datagridview? Any help greatly appreciated! Thanks in advance
1
1923
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 value for each of the selected rows). My question is, how do I loop through the selected rows and reference the ID column in the data grid? I can get the count of selected columns, but I don't know how to reference the data in the rows. Thanks.
2
1072
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 will change? Pls help. Thank in advance. Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " &...
1
4119
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 myTable is boolean (two state - without null value). How can I change value in this column after click on row in datagridview (one of cells in rows).
1
1516
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, "Wybierz pliki", 0, True)
0
1005
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
1849
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) and then add the value created by button on mainform to these selected rows in the subform at once. Mainform = racef Subform = timesf field in subform = timestart i currently use this code to add the value created by button on mainform,...
0
9803
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10808
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7766
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4433
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3088
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.