vb.net Visual Basic,如何读取数据网格中的每一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1031704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:16:35  来源:igfitidea点击:

Visual Basic, How do I read each row in a datagrid?

vb.netdatagridview

提问by Patrick McDonald

I have a datagrid called DataGridView1, column A contains a name, column B contains a path to a file. How do I run some code for each row? What is the correct terminology for traversing a datagrid in this way?

我有一个名为 DataGridView1 的数据网格,A 列包含一个名称,B 列包含一个文件路径。如何为每一行运行一些代码?以这种方式遍历数据网格的正确术语是什么?

Example of what I need:

我需要的示例:

For each row in DataGridView1
 MessageBox.Show DataGridView1.ColumnA.text & "," & DataGridView1.ColumnB.text

Thanks

谢谢

回答by Patrick McDonald

You were nearly there, you need something like the following:

你快到了,你需要像下面这样的东西:

For Each row As DataGridViewRow In DataGridView1.Rows
    If Not row.IsNewRow Then
        MessageBox.Show(row.Cells(0).Value.ToString & "," & row.Cells(1).Value.ToString)
    End If
Next

EDIT:

编辑:

You need to check if the row.IsNewRow is not True if your DataGridView allows adding rows.

如果您的 DataGridView 允许添加行,您需要检查 row.IsNewRow 是否不为 True。