dr["num"] = "i.tostring";
时间: 2023-08-27 13:12:21 浏览: 37
你给一个名为 `dr` 的对象的 `num` 属性赋值为 `"i.tostring"`。这里的语法看起来是 C# 中的语法,假设 `dr` 是一个 DataRow 对象。`i.tostring` 可能是一个字符串,但它看起来更像是一个方法调用而不是一个字符串字面量。如果你想将 `i` 变量转换为一个字符串来赋值给 `dr` 对象的 `num` 属性,你可以使用 `ToString()` 方法,而不是将其作为字符串字面量使用。例如,你可以这样写:
```csharp
dr["num"] = i.ToString();
```
这将将 `i` 变量的字符串表示赋值给 `dr` 对象的 `num` 属性。
相关问题
dataInfo = new DeviceManage.设备采购(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Selected == true) { if (isadmin == "1") { if (row.Cells[0].Value == null) { dataInfo.buy_no = cgdh.Text = dataGridView1.Rows[i].Cells[2].Value.ToString(); dataInfo.login_no = yhbh.Text = dataGridView1.Rows[i].Cells[3].Value.ToString(); dataInfo.dev_name = xbmc.Text = dataGridView1.Rows[i].Cells[4].Value.ToString(); dataInfo.dev_model = xbxh.Text = dataGridView1.Rows[i].Cells[5].Value.ToString(); dataInfo.gys_name = gys.Text = dataGridView1.Rows[i].Cells[6].Value.ToString(); dataInfo.buy_date = dateTimePicker1.Text = dataGridView1.Rows[i].Cells[7].Value.ToString(); dataInfo.price = dj.Text = dataGridView1.Rows[i].Cells[8].Value.ToString(); dataInfo.num = sl.Text = dataGridView1.Rows[i].Cells[9].Value.ToString(); }
将上述代码中的 `for` 循环改为 `foreach` 循环,并将代码中的索引变量 `i` 替换为 `row.Index`,可以使用以下代码:
```csharp
dataInfo = new DeviceManage.设备采购();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Selected == true)
{
if (isadmin == "1")
{
if (row.Cells[0].Value == null)
{
dataInfo.buy_no = cgdh.Text = row.Cells[2].Value.ToString();
dataInfo.login_no = yhbh.Text = row.Cells[3].Value.ToString();
dataInfo.dev_name = xbmc.Text = row.Cells[4].Value.ToString();
dataInfo.dev_model = xbxh.Text = row.Cells[5].Value.ToString();
dataInfo.gys_name = gys.Text = row.Cells[6].Value.ToString();
dataInfo.buy_date = dateTimePicker1.Text = row.Cells[7].Value.ToString();
dataInfo.price = dj.Text = row.Cells[8].Value.ToString();
dataInfo.num = sl.Text = row.Cells[9].Value.ToString();
}
}
}
}
```
在 `foreach` 循环中,`row` 直接表示集合中的每个元素,可以直接使用 `row.Cells[0]` 访问该行中的第一列单元格。使用 `row.Index` 可以获取当前行的索引。注意,`foreach` 循环中的变量是只读的,不能修改它们的值,因此需要使用 `row.Index` 来代替原来的索引变量 `i`。
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(); } }转化成不一样的代码
以下是可能的另一种实现方式:
```
if (dataGridView1.SelectedRows.Count > 0) // 检查是否有选中的行
{
DataGridViewRow row = dataGridView1.SelectedRows[0]; // 获取第一行
dataInfo = new 用户基本信息
{
No = yhbh.Text = row.Cells[0].Value.ToString(),
Name = yhmc.Text = row.Cells[1].Value.ToString(),
Password = yhmm.Text = row.Cells[2].Value.ToString(),
Dept = bm.Text = row.Cells[3].Value.ToString(),
Job = textBox11.Text = row.Cells[4].Value.ToString(),
Nation = zw.Text = row.Cells[5].Value.ToString(),
Birthday = dateTimePicker1.Text = row.Cells[6].Value.ToString(),
Phone = dh.Text = row.Cells[7].Value.ToString(),
Mail = yx.Text = row.Cells[8].Value.ToString()
};
}
```
该代码片段首先检查是否有选中的行,然后获取第一行,并将其数据赋值给 `dataInfo` 对象和窗体上的文本框。注意,这里使用了对象初始化器来构造 `dataInfo` 对象。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)