A. Petit
A. Petit

Reputation: 158

Parsing a cell value from a column with modified HeaderText

I'm parsing value from some cells in a DGV like this :

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.columnIndex == 17)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (int.TryParse(row.Cells[17].Value.ToString(), out result))
            {
                qtyFailed = Convert.ToInt32(row.Cells[17].Value);
            }
            else
            {
                qtyFailed = 0;
            }
            // Modify some stuff in the row depending on qtyFailed value
        }
    }
}

I need qtyFailed to calculate some values and check if the user did not input some invalid informations.
It works fine but if I modify this column HeaderText like this :

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    dataGridView1.Columns[17].HeaderText = "Failed";
}

I will get a System.ArgumentOutOfRangeException when the app starts.

Also, even if the error is raised, the column HeaderText is changed and the cell value can be parsed as intended.
Commenting out the Headertext change removes the error.

I don't really understand what is going on.

Upvotes: 1

Views: 33

Answers (1)

Berkay Yaylacı
Berkay Yaylacı

Reputation: 4513

Try this,

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) {
       DataGridViewColumn result = dataGridView1.Columns.Cast<DataGridViewColumn>().FirstOrDefault(x => x.Index == 17);
       if(result != null) {
           result.HeaderText = "Failed";
       }
}

Hope helps,

Upvotes: 1

Related Questions