如何通过for循环读取DatagridView的行 [英] How to read Rows of a DatagridView through for loop

查看:119
本文介绍了如何通过for循环读取DatagridView的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的应用程序是在Vb.net中编码的VS2008中.我有一个从数据库填充的datagridview.在具有DatagridView的同一窗体上有一个保存按钮.
我的要求是我希望当用户单击保存时,DatagridView中填充的所有记录都应保存在数据库的某个表中.
我已经完成了保存和更新代码.我无法做的是,当单击保存"按钮后执行For循环时,无法增加计数器并移至datagridView的下一行.
以下是我的代码.

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.

For i As Integer = 0 To DGVStudRecord.RowCount - 1
My Code for Update and Save.
Next


这里发生的是单击保存按钮时,我的for循环正在执行并循环直到RowCount-1.但是我希望在每个循环完成后,我应该到达datagirdView中的下一行.将下一行的值传递给进行更新和删除操作的控件.
单击保存按钮,datagridview中的所有数据将保存在某个表中.
请提出建议.
以下是我的实际代码.
我想将光标移到下一行并为我的更新查询提供相应的列值.


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.

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

推荐答案

在MSDN上查找datagridview.将RowIndex从0改为(RowCount -1),您将获得所需的效果.

这是您需要注意的典型样本

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];



这是C ++/CLI,但是MSDN可以选择在可用的位置查看VB等效项.



This is C++/CLI, but MSDN will have an option to see a VB equivalent where it is available.


要遍历DataGridView,请尝试如下操作:

代码段

To loop through a DataGridView, try something like this:

Code Snippet

For Each row As DatagridViewRow In myDataGridView.Rows
    MessageBox.Show(CStr(row.Cells(1).FormattedValue))
Next row


frnds
得到了解决方案.
只需添加i代替
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


这篇关于如何通过for循环读取DatagridView的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆