vb.net DataGridView使用循环指定多个列标题文本获取多列单元格的字符串的多种方法,每行后面注释
时间: 2024-01-14 21:03:28 浏览: 87
1. 使用循环遍历每个指定的列,逐一获取单元格的字符串:
```
For Each col As DataGridViewColumn In DataGridView1.Columns
If col.HeaderText = "Column1" OrElse col.HeaderText = "Column2" Then
For Each row As DataGridViewRow In DataGridView1.Rows
Dim cellValue As String = row.Cells(col.Index).Value.ToString()
'处理单元格的字符串
Next
End If
Next
```
2. 使用 LINQ 查询语句获取指定列的单元格字符串:
```
Dim result = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)()
Select New With {
.Column1 = row.Cells("Column1").Value.ToString(),
.Column2 = row.Cells("Column2").Value.ToString()
}
For Each item In result
'处理每个单元格的字符串
Next
```
3. 使用 GetCellCount 方法和 GetCellValues 方法获取指定列的单元格字符串:
```
Dim col1Index As Integer = DataGridView1.Columns("Column1").Index
Dim col2Index As Integer = DataGridView1.Columns("Column2").Index
Dim rowCount As Integer = DataGridView1.Rows.Count
Dim cellValues = DataGridView1.GetCellValues(col1Index, 0, rowCount, col2Index + 1)
For Each rowValues In cellValues
Dim col1Value As String = rowValues(col1Index).ToString()
Dim col2Value As String = rowValues(col2Index).ToString()
'处理每个单元格的字符串
Next
```
阅读全文