for (int k = 0; k < cell; k++) { if (DataGridView.Columns[k].HeaderText == "子地址") continue; int j = k; if (DataGridView.Columns[k].HeaderText == m_StatusTable[0][j]) DataGridView.Rows[i - 1].Cells[k].Value = m_StatusTable[i][j]; } 想要当DataGridView.Columns[k].HeaderText == "子地址"时候,k++,但是j不变,然后接下来是k++,新的k值,和原本的k值也就是不变的j值两者进行比较
时间: 2024-02-14 15:08:47 浏览: 79
您想要实现的功能,可以将代码改成如下形式:
```
for (int k = 0; k < cell; k++) {
if (DataGridView.Columns[k].HeaderText == "子地址") {
k++;
continue;
}
int j = k;
if (DataGridView.Columns[k].HeaderText == m_StatusTable[0][j]) {
DataGridView.Rows[i - 1].Cells[k].Value = m_StatusTable[i][j];
}
}
```
这样,当 `DataGridView.Columns[k].HeaderText == "子地址"` 时,`k` 的值会增加 1,而 `j` 的值不变,接下来再进行下一轮循环时,`k` 的值会和原本的 `k` 值进行比较,而 `j` 的值不变,实现您想要的功能。
相关问题
for (int k = 0; k < cell; k++) { int j = k; if (DataGridView.Columns[k].HeaderText == "子地址") { j = k + 1; } if (DataGridView.Columns[k].HeaderText == m_StatusTable[0][j]) DataGridView.Rows[i - 1].Cells[k].Value = m_StatusTable[i][j]; }想要实现当DataGridView.Columns[k].HeaderText == "子地址"时,让后面的DataGridView.Rows[i-1].Cells[k]中的K值加一,而j值不变,做到 DataGridView.Rows[i - 1].Cells[k].Value = m_StatusTable[i][j];同时在之后的每一次循环里面,都让k比j大一
可以将代码改为如下形式实现这个功能:
```
for (int k = 0, j = 0; k < cell; k++, j++) {
if (DataGridView.Columns[k].HeaderText == "子地址") {
j++;
}
if (DataGridView.Columns[k].HeaderText == m_StatusTable[0][j]) {
DataGridView.Rows[i - 1].Cells[k].Value = m_StatusTable[i][j];
}
}
```
通过在循环内部定义变量j并让它与k保持一致,如果当前列的HeaderText为"子地址",则将j增加1,这样在之后的每一次循环里面,k比j大1,而j保持不变,实现了DataGridView.Rows[i - 1].Cells[k]中的K值加一,而j值不变的效果。最后,如果当前列的HeaderText与m_StatusTable[0][j]相等,则将m_StatusTable[i][j]赋值给DataGridView.Rows[i - 1].Cells[k]。
C# datagridview 每行用不同的cell类型
在C#的DataGridView控件中,如果你想要每行使用不同的单元格类型,这通常是在数据模型复杂、需要展示不同类型的数据时实现的。以下是一种常见的做法:
1. 首先,确保你的数据源是一个可以支持不同类型字段的对象集合,比如DataTable、List<Dictionary<string, object>> 或自定义的Poco类。
2. 在设计表头时,添加多个列(DataGridViewColumn),每个列代表一种类型的字段。为每种类型设置合适的DefaultCellStyle属性,如DataGridViewTextBoxColumn用于文本字段,DataGridViewTextBoxColumn用于数值字段,DataGridViewCheckBoxColumn用于布尔值等。
```csharp
DataGridViewTextBoxColumn textCol = new DataGridViewTextBoxColumn();
textCol.HeaderText = "Text Field";
textCol.DefaultCellStyle = new DataGridViewCellStyle(); // 设置文本样式
DataGridViewComboBoxColumn comboBoxCol = new DataGridViewComboBoxColumn();
comboBoxCol.HeaderText = "Dropdown Field";
comboBoxCol.DataSource = someDataSource; // 设置下拉框选项
dataGridView.Columns.Add(textCol);
dataGridView.Columns.Add(comboBoxCol);
```
3. 当绑定数据到DataGridView时,遍历数据源并动态创建DataGridViewCells,将它们添加到对应的行上,确保单元格类型与列类型匹配。
```csharp
foreach (var dataItem in dataSource)
{
DataGridViewRow row = dataGridView.Rows.Add();
foreach (KeyValuePair<string, object> keyValue in dataItem)
{
var cell = row.Cells[dataGridView.Columns.IndexOf(keyValue.Key)];
switch (keyValue.Value.GetType().Name) // 根据数据类型设置单元格
{
case "string":
cell.Value = keyValue.Value.ToString();
break;
case "int":
cell.Value = Convert.ToInt32(keyValue.Value);
cell.Style.Format = "N0"; // 如果需要格式化数字
break;
// 添加更多case...
}
}
}
```
阅读全文
相关推荐
















