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-21 12:04:46 浏览: 122
可以改写成 foreach 循环的形式,如下所示:
```C#
dataInfo = new DeviceManage.设备报废();
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
if (isadmin == "1" && 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();
}
}
```
这里使用了 foreach 循环遍历 dataGridView1 中选中的行,对于每一行,判断是否为管理员且第一列数据为 null,如果成立,则将选中行的各列数据赋值给 dataInfo 对象的属性,并同时赋值给界面上的几个文本框。
相关问题
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 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` 对象中。同义改写后的代码与原代码的功能相同,只是稍微重构了一下。
阅读全文