如何在DataBindingComplete方法中重新格式化所有单元格?

时间:2017-08-21 20:16:14

标签: c# winforms c#-4.0 datagridview

我需要按名称迭代所有行/单元格,并在方法中用条件替换值: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

1 个答案:

答案 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") ? "Повторно" : "Первый раз";
    }
}