Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
VB
For i As Integer = 0 To DGVStudRecord.RowCount - 1
My Code for Update and Save.
Next

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.
Below is my actual Code.
I want to cursor to move to next row and provide respective colum values to my update query.
VB
<pre> For i As Integer = 0 To DGVStudRecord.RowCount - 1
           
            DBConnect()
            Dim strsql1 As String = ""
            con = DBActions.DBConnect
            strsql1 = "Select No from TableName where No='" & DGVStudRecord.Item(0, DGVStudRecord.CurrentCell.RowIndex).Value & "'"
            cmd = New SqlCommand(strsql1, con)
            dr = cmd.ExecuteReader
            If dr.HasRows = True Then
               
                dr.Close()
                strsql = "Update TableName set No='" & DGVStudRecord.Item(1, DGVStudRecord.CurrentCell.RowIndex).Value & "'," & _
                      "Column1='" & DGVStudRecord.Item(2, DGVStudRecord.CurrentCell.RowIndex).Value & "'," & _
                      "Column2='" & DGVStudRecord.Item(3, DGVStudRecord.CurrentCell.RowIndex).Value & "'," & _
                      "Column3='" & DGVStudRecord.Item(4, DGVStudRecord.CurrentCell.RowIndex).Value & "'," & _
                      "Column4='" & DGVStudRecord.Item(6, DGVStudRecord.CurrentCell.RowIndex).Value & "'," & _
                    
                "where No='" & DGVStudRecord.Item(0, DGVStudRecord.CurrentCell.RowIndex).Value & "'"
                cmd = New SqlCommand(strsql, con)
                cmd.ExecuteNonQuery()
                dr.Close()
             
            End If

End If

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.
Posted
Updated 11-Sep-11 19:34pm
v2

Look up datagridview on MSDN. Work the RowIndex from 0 to (RowCount -1) and you will get the effect you need.

Here's a typical sample of what you need to watch for

DGVStudRecord->Rows[e->RowIndex]->Cells[e->ColumnIndex];


This is C++/CLI, but MSDN will have an option to see a VB equivalent where it is available.
 
Share this answer
 
v2
To loop through a DataGridView, try something like this:

Code Snippet

VB
For Each row As DatagridViewRow In myDataGridView.Rows
    MessageBox.Show(CStr(row.Cells(1).FormattedValue))
Next row
 
Share this answer
 
hi frnds
got the solution.
just needed to add i in place of
DGVStudRecord.Item(0, DGVStudRecord.CurrentCell.RowIndex).Value

DGVStudRecord.Item(0,i).Value
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900