我需要按名称迭代所有行/单元格,并在方法中用条件替换值:DataBindingComplete
。
我试着这样做:
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
string pass = dataGridView1.Rows[e.RowIndex].Cells["dataGridViewTextBoxColumn45"].Value.ToString();
dataGridView1.Rows[e.RowIndex].Cells["dataGridViewTextBoxColumn45"].Value = (pass == "1") ? "Повторно" : "Первый раз";
}
但是没有e.RowIndex
属性。怎么做?
不建议我使用方法_CellFormatting
。它对我不起作用,因为我有自定义列,可以通过这种方法呈现。
我也是这样试过的:
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) {
foreach (DataGridViewRow row in dataGridView1.Rows)
{
intRow++;
string pass = dataGridView1.Rows[intRow].Cells["dataGridViewTextBoxColumn45"].Value.ToString();
dataGridView1.Rows[intRow].Cells["dataGridViewTextBoxColumn45"].Value = (pass == "1") ? "Повторно" : "Первый раз";
}
}
它返回错误:
发生了System.StackOverflowException HResult = 0x800703E9
答案 0 :(得分:1)
foreach循环中不需要索引:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0] is DataGridViewTextBoxCell)
{
string pass = ((DataGridViewTextBoxCell)row.Cells[0]).Value;
((DataGridViewTextBoxCell)row.Cells[0]).Value = (pass == "1") ? "Повторно" : "Первый раз";
}
}