How to reformat all cells in DataBindingComplete method?

0

I need to iterate all rows/cells by name and replace value by condition in method: DataBindingComplete.

I tried to to this like as:

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

But there is no e.RowIndex property. How to do that?

Dont suggest me to use method _CellFormatting. It does not work for me, because I have got custom columns, that can be rendered by this method.

Also I tried this way:

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

It returns an error:

System.StackOverflowException occurred HResult=0x800703E9

c#
winforms
c#-4.0
datagridview
asked on Stack Overflow Aug 21, 2017 by OPV • edited Aug 21, 2017 by OPV

1 Answer

1

You don't need an index inside a foreach loop:

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") ? "Повторно" : "Первый раз";
    }
}
answered on Stack Overflow Aug 21, 2017 by Isma • edited Aug 22, 2017 by Isma

User contributions licensed under CC BY-SA 3.0