循环dataGridView1.SelectedRows
时间: 2023-06-21 11:09:20 浏览: 72
循 dataGridView1.SelectedRows 可以使用 foreach 循环,代码如下:
```
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
// 在这里处理每一行数据
}
```
在循环体中,可以使用 row.Cells[index] 访问每一列的值,其中 index 是列的索引,从 0 开始。例如,如果要访问第一列的值,可以使用 row.Cells[0]。完整的代码示例如下:
```
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
string value = row.Cells[0].Value.ToString(); // 访问第一列的值
// 在这里处理每一行数据
}
```
相关问题
dataInfo = new DeviceManage.设备报废(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (dataGridView1.Rows[i].Selected == true) { if (isadmin == "1") { if (dataGridView1.Rows[i].Cells[0].Value == null) { dataInfo.sp_no = bfdh.Text = dataGridView1.Rows[i].Cells[2].Value.ToString(); dataInfo.login_no = yhbh.Text = dataGridView1.Rows[i].Cells[3].Value.ToString(); dataInfo.dev_no = sbbh.Text = dataGridView1.Rows[i].Cells[4].Value.ToString(); dataInfo.sp_why = bfyy.Text = dataGridView1.Rows[i].Cells[5].Value.ToString(); dataInfo.remark = bz.Text = dataGridView1.Rows[i].Cells[6].Value.ToString(); } 改成foreach
将上面的代码改为foreach循环,可以按照如下方式实现:
```
dataInfo = new DeviceManage.设备报废();
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
if (isadmin == "1")
{
if (row.Cells[0].Value == null)
{
dataInfo.sp_no = bfdh.Text = row.Cells[2].Value.ToString();
dataInfo.login_no = yhbh.Text = row.Cells[3].Value.ToString();
dataInfo.dev_no = sbbh.Text = row.Cells[4].Value.ToString();
dataInfo.sp_why = bfyy.Text = row.Cells[5].Value.ToString();
dataInfo.remark = bz.Text = row.Cells[6].Value.ToString();
}
}
}
```
这样就可以遍历选中的DataGridView行,将每一行的数据存入对应的变量中。
dataInfo = new 用户基本信息(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (dataGridView1.Rows[i].Selected == true) { dataInfo.No = yhbh.Text = dataGridView1.Rows[i].Cells[0].Value.ToString(); dataInfo.Name = yhmc.Text = dataGridView1.Rows[i].Cells[1].Value.ToString(); dataInfo.Password = yhmm.Text = dataGridView1.Rows[i].Cells[2].Value.ToString(); dataInfo.Dept = bm.Text = dataGridView1.Rows[i].Cells[3].Value.ToString(); dataInfo.Nation = zw.Text = dataGridView1.Rows[i].Cells[5].Value.ToString(); dataInfo.Job = textBox11.Text = dataGridView1.Rows[i].Cells[4].Value.ToString(); dataInfo.Birthday = dateTimePicker1.Text = dataGridView1.Rows[i].Cells[6].Value.ToString(); dataInfo.Phone = dh.Text = dataGridView1.Rows[i].Cells[7].Value.ToString(); dataInfo.Mail = yx.Text = dataGridView1.Rows[i].Cells[8].Value.ToString(); } }
这段代码通过遍历 `dataGridView1` 控件的所有行,查找选中的行并将选中的行的值赋值给 `dataInfo` 对象中的成员变量。具体实现步骤如下:
1. 创建一个 `用户基本信息` 对象 `dataInfo`,用于保存选中的行的值;
2. 遍历 `dataGridView1` 控件的所有行,判断当前行是否被选中;
3. 如果当前行被选中,将该行的值赋值给 `dataInfo` 对象中的成员变量;
4. 将 `dataInfo` 对象中的成员变量的值分别设置为对应的文本框、下拉框等控件的值。
需要注意的是,这段代码中存在以下问题:
1. 在遍历 `dataGridView1` 控件的所有行时,应该使用 `foreach` 循环而不是 `for` 循环,因为 `foreach` 循环更加简洁易读;
2. 在每次循环中,应该先判断当前行是否被选中,再进行赋值操作,否则会导致 `dataInfo` 对象中的成员变量被错误地赋值;
3. 在将 `dataInfo` 对象中的成员变量的值赋值给对应的控件时,应该使用控件的属性来设置其值,而不是将控件的 `Text` 属性赋值给 `dataInfo` 对象中的成员变量。
改正以上问题后的代码如下所示:
```csharp
dataInfo = new 用户基本信息();
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
dataInfo.No = yhbh.Text = row.Cells[0].Value.ToString();
dataInfo.Name = yhmc.Text = row.Cells[1].Value.ToString();
dataInfo.Password = yhmm.Text = row.Cells[2].Value.ToString();
dataInfo.Dept = bm.Text = row.Cells[3].Value.ToString();
dataInfo.Nation = zw.Text = row.Cells[5].Value.ToString();
dataInfo.Job = textBox11.Text = row.Cells[4].Value.ToString();
dataInfo.Birthday = dateTimePicker1.Value = Convert.ToDateTime(row.Cells[6].Value);
dataInfo.Phone = dh.Text = row.Cells[7].Value.ToString();
dataInfo.Mail = yx.Text = row.Cells[8].Value.ToString();
}
```
这样就可以正确地将选中的行的值赋值给 `dataInfo` 对象,并将值显示在对应的控件中。
相关推荐
![](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)