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
时间: 2024-01-26 18:02:23 浏览: 77
将上面的代码改为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 DeviceManage.设备运行(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (dataGridView1.Rows[i].Selected == true) { dataInfo.dev_no = sbbh.Text = dataGridView1.Rows[i].Cells[0].Value.ToString(); dataInfo.date = dateTimePicker1.Text = dataGridView1.Rows[i].Cells[1].Value.ToString(); dataInfo.starttime = dateTimePicker2.Text = dataGridView1.Rows[i].Cells[2].Value.ToString(); dataInfo.endtime = dateTimePicker3.Text = dataGridView1.Rows[i].Cells[3].Value.ToString(); dataInfo.endwhy = tjyy.Text = dataGridView1.Rows[i].Cells[4].Value.ToString(); dataInfo.devstatus = yxzt.Text = dataGridView1.Rows[i].Cells[5].Value.ToString(); dataInfo.remark = bz.Text = dataGridView1.Rows[i].Cells[6].Value.ToString(); } 同义改写
以下是对上述代码的同义改写:
```
DeviceManage.设备运行 dataInfo = new DeviceManage.设备运行();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Selected == true)
{
sbbh.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
dateTimePicker1.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
dateTimePicker2.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
dateTimePicker3.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
tjyy.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
yxzt.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
bz.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
dataInfo.dev_no = sbbh.Text;
dataInfo.date = dateTimePicker1.Text;
dataInfo.starttime = dateTimePicker2.Text;
dataInfo.endtime = dateTimePicker3.Text;
dataInfo.endwhy = tjyy.Text;
dataInfo.devstatus = yxzt.Text;
dataInfo.remark = bz.Text;
}
}
```
这个代码段的作用是将选中的行中的数据填充到表单上,并将该行数据保存在 `dataInfo` 对象中。同义改写后的代码与原代码的功能相同,只是稍微重构了一下。
dataInfo = new DeviceManage.故障管理(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (dataGridView1.Rows[i].Selected == true) { dataInfo.ft_no = bxdh.Text = dataGridView1.Rows[i].Cells[0].Value.ToString(); dataInfo.login_no = yhbh.Text = dataGridView1.Rows[i].Cells[1].Value.ToString(); dataInfo.dev_no = xbbh.Text = dataGridView1.Rows[i].Cells[2].Value.ToString(); dataInfo.ftwhy = gzyy.Text = dataGridView1.Rows[i].Cells[3].Value.ToString(); dataInfo.ftstatus = gzztms.Text = dataGridView1.Rows[i].Cells[4].Value.ToString(); dataInfo.remark = bz.Text = dataGridView1.Rows[i].Cells[5].Value.ToString(); } }改写成doreach
可以这样改写:
```
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
dataInfo = new DeviceManage.故障管理();
dataInfo.ft_no = bxdh.Text = row.Cells[0].Value.ToString();
dataInfo.login_no = yhbh.Text = row.Cells[1].Value.ToString();
dataInfo.dev_no = xbbh.Text = row.Cells[2].Value.ToString();
dataInfo.ftwhy = gzyy.Text = row.Cells[3].Value.ToString();
dataInfo.ftstatus = gzztms.Text = row.Cells[4].Value.ToString();
dataInfo.remark = bz.Text = row.Cells[5].Value.ToString();
}
```
这样,遍历选中行的过程中,每次都会新创建一个 `dataInfo` 对象,并将选中行的对应值赋值给它。
阅读全文