473,513 Members | 3,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read Rows of a DatagridView through for loop

45 New Member
Hi,
My application is in VS2008 coded in Vb.net.I have a datagridview which is populated from Database.I have a save button on the same form that has DatagridView.
My requirement is i want that all the records that are populated in DatagridView should be saved in a certain table of the database when the user clicks save.
Im done with my save and Update code.What im not able to do is,im not able to increment the counter and move to the next row of datagridView when my For loop is executing after the click of Save button.
Below is my Code.
Expand|Select|Wrap|Line Numbers
  1. For i As Integer = 0 To DGVStudRecord.RowCount - 1
  2. My Code for Update and Save.
  3. Next
  4.  
Here what is happening is when save button is clicked my for Loop is executing and looping till RowCount-1.But i want that after completion of each loop my i should get to the next row in the datagirdView.I should be able to get the values of next row to pass it to my controls that are carrying update and delete operation.
The entire data in the datagridview will be saved in a certain table at the click of save button.
Please suggest.
Sep 10 '11 #1
3 10115
yarbrough40
320 Contributor
Try
Expand|Select|Wrap|Line Numbers
  1.  
  2. DGVStudRecord.Rows(i).Cells(0).Text
  3.  
Sep 10 '11 #2
SEhtesham
45 New Member
Below is my actual Code.
I want the cursor to move to next row and provide respective column values to my update query.
Expand|Select|Wrap|Line Numbers
  1. For i As Integer = 0 To DGVStudRecord.RowCount - 1
  2.  
  3.             DBConnect()
  4.             Dim strsql1 As String = ""
  5.             con = DBActions.DBConnect
  6.             strsql1 = "Select No from TableName where No='" & DGVStudRecord.Item(0, DGVStudRecord.CurrentCell.RowIndex).Value & "'"
  7.             cmd = New SqlCommand(strsql1, con)
  8.             dr = cmd.ExecuteReader
  9.             If dr.HasRows = True Then
  10.  
  11.                 dr.Close()
  12.                 strsql = "Update TableName set No='" & DGVStudRecord.Item(1, DGVStudRecord.CurrentCell.RowIndex).Value & "'," & _
  13.                       "Column1='" & DGVStudRecord.Item(2, DGVStudRecord.CurrentCell.RowIndex).Value & "'," & _
  14.                       "Column2='" & DGVStudRecord.Item(3, DGVStudRecord.CurrentCell.RowIndex).Value & "'," & _
  15.                       "Column3='" & DGVStudRecord.Item(4, DGVStudRecord.CurrentCell.RowIndex).Value & "'," & _
  16.                       "Column4='" & DGVStudRecord.Item(6, DGVStudRecord.CurrentCell.RowIndex).Value & "'," & _
  17.  
  18.                 "where No='" & DGVStudRecord.Item(0, DGVStudRecord.CurrentCell.RowIndex).Value & "'"
  19.                 cmd = New SqlCommand(strsql, con)
  20.                 cmd.ExecuteNonQuery()
  21.                 dr.Close()
  22.             End If
  23. End If  
  24.  
Please dont look for code it may contain error as i have truncated it.Just tell me how can i get the new row values to give it to query parameter to update.
Sep 12 '11 #3
SEhtesham
45 New Member
Thanks frnds for ur support.
i got the solution to my query.
i just needed to add i in place of DGVStudRecord.CurrentCell.RowIndex.
Expand|Select|Wrap|Line Numbers
  1. DGVStudRecord.Item(0,i).Value
  2.  
Sep 12 '11 #4

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

Similar topics

6
35451
by: Dan | last post by:
I'd like to loop through selected datagrid rows and extract specified columns from those rows. How would I do that? Thanks... Dan
2
1014
by: Sascha Meyer | last post by:
Hi there, I've tried several ways to resolve this problem and also searched the net but I'm still not satisfied with my different solutions: how do I check for rows in a DataReader in a CompactFramework Appication with SQL Server CE? I tried something like sqlSCom.CommandText = "SELECT somerows FROM sometable"
2
3424
by: nekiv90 | last post by:
Greetings, How can 'rows read' from a DB2 CALL statement incur so many reads? I would expect it to be zero. Could someone enlighten me on this? Thanks! Here is output from executing event monitor on STATEMENTS: 68) Statement Event ...
2
28089
by: Philip Wagenaar | last post by:
I have a dataset that I queried from an excel sheet. I want to loop through the rows, then loop through the columns and write them to a file. But I got stuck pretty quickly: So far: For Each dr As DataRow In Order.Tables(0).Rows Next
5
1536
by: JohnK | last post by:
from what I read & tried, DataGridView was meant for Window Forms, not for ASP/Web pages... correct? While I can use it to display data, I can't seem to figure out how to edit the cells... I'm not looking to put edit buttons on each row... I'll go back to regular window forms before I create such an interface.. So is it true, in ASP.Net...
1
1252
by: jjmurali | last post by:
hi guys i have one problem with datagridview.. i m displaying data in datagridview in c# application. i added one checkbox column to datagridview ..as well as i have one button(Select All), whenever i click the button all check boxes checked in checkboxes column. i have another button(Remove) whenever i click the button remove the selected...
0
1257
by: Hetal | last post by:
Hi there.. I am a VB6 developer so kinda trying to figure how to work with VB.NET. I have a scenario where i have a DataSet bound to a combo box, and i would like to read a row from the DataSet based on the item selected in the Combo Box. For e.g. I have a dataset that has 1 table containing 3 fields say ID, Name, and Address.
0
921
by: obrienkev | last post by:
Hi all, I have used the below to display the cell content of the datagridview cell the user clicked on. However, the MessageBox is displayed twice.
2
1871
by: gm41970 | last post by:
I have a datagridview in a .NET C# app that I have set to be read-only (grdXXXX.ReadOnly = true) since I do not want users to edit data that is already there. But I need to allow user to insert new row and add new info. I can add the row (using Columns.AddRange() but since the datagridview is set to read-only, cannot actually type the data into...
2
11579
by: cday119 | last post by:
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...
0
7270
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...
0
7178
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...
1
7125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5102
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...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
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
0
470
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...

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.