vb.net DataGridView将指定几行的文本添加到数组并访问,每行代码后面注释
时间: 2024-03-26 16:36:29 浏览: 26
好的,以下是示例代码:
```
' 声明一个字符串数组用于存储文本
Dim rowValues As String() = New String() {}
' 遍历选定的行
For Each selectedRow As DataGridViewRow In DataGridView1.SelectedRows
' 声明一个字符串列表用于存储当前行的文本
Dim currentRowValues As New List(Of String)()
' 遍历当前行的单元格
For Each cell As DataGridViewCell In selectedRow.Cells
' 将当前单元格的文本添加到列表中
currentRowValues.Add(cell.Value.ToString())
Next
' 将当前行的文本转换为数组并添加到主数组中
rowValues = rowValues.Concat(currentRowValues.ToArray()).ToArray()
Next
' 输出数组中的文本
For Each value As String In rowValues
Console.WriteLine("Selected row value: " & value)
Next
```
解释:
首先,我们声明了一个字符串数组 `rowValues` 用于存储选定行的文本。
然后,我们使用 `For Each` 循环遍历 `SelectedRows` 集合中的每一行。在循环中,我们声明了一个字符串列表 `currentRowValues` 用于存储当前行的文本。
接着,我们再次使用 `For Each` 循环遍历当前行的所有单元格。在内部循环中,我们将当前单元格的文本添加到列表 `currentRowValues` 中。
在完成当前行的遍历后,我们将列表 `currentRowValues` 转换为字符串数组,并将其添加到主数组 `rowValues` 中。为此,我们使用 `Concat` 方法将两个数组连接起来,并使用 `ToArray` 方法将结果转换为数组。最后,我们将得到的数组重新赋值给 `rowValues`。
最后,我们使用 `For Each` 循环输出数组中的所有文本。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)