0

I'm trying to show the row number on DataGridView RowHeader, but when I do the following block of code it doesn't change the first row row.HeaderCell.Value, but it will change all the next. When I watch the row variable and the gridView.Rows[0], the gridView.Rows[0] doesn't change at all and the row gets clear like it's a new row.

foreach (DataGridViewRow row in gridView.Rows)
{
    row.HeaderCell.Value = (row.Index + 1).ToString();
}

Weirdly, if I change the gridView.Rows[0].HeaderCell.Value before entering the loop, this line will be "ignored", but the loop will work just fine and change the HeaderCells of all rows.

gridView.Rows[0].HeaderCell.Value = "";
foreach (DataGridViewRow row in gridView.Rows)
{
    row.HeaderCell.Value = (row.Index + 1).ToString();
}

It looks like you need to change the first row once to "unlock" the subsequent changes.

What could be going on to this issue?

Thank you all

EDIT:

This is a Windows Application on .NET Framework 4.8 And I forgot to show the steps before the problem.

bs = new BindingSource();
bs.DataSource = table;
IBindingListView bindingList = bs;
gridView.DataSource = bindingList;
gridView.Visible = true;

foreach (DataGridViewRow row in gridView.Rows)
{
    row.HeaderCell.Value = (row.Index + 1).ToString();
}
  • Does changing the foreach to a for-loop change anything? Like: 'for (int i = 0; i < gridView.Rows.Count; i++) { gridViews.Rows[i].HeaderCell.Value = (i + 1).ToString(); }' – Tim Schmelter Aug 25 '22 at 15:54
  • See if the [following](https://stackoverflow.com/a/12840794/5509738) might work for you – Karen Payne Aug 25 '22 at 18:22
  • .NET Framework or .NET Core? which version? I presume this is WinForms, correct? – CoolBots Aug 25 '22 at 19:43
  • @TimSchmelter I've already tried this. It happens the same, just the first row don't change and the next ones are fine using foreach or for-loop – Igor Micadei Aug 25 '22 at 20:09
  • @CoolBots Correct. Windows Application on .NET Framework 4.8 – Igor Micadei Aug 25 '22 at 20:11
  • @KarenPayne I didn't try this method because I was trying a way to just change the value of HeaderCell insted of drawing over the header. – Igor Micadei Aug 25 '22 at 20:13
  • Don't call the numbering loop right after assigning value to the `.DataSource` property. If you set the `.DataSource` in the constructor, then move the loop part to the `Load` event. You need to wait until all grid's handles and objects are created. – dr.null Aug 25 '22 at 22:20

1 Answers1

0

If you don't have a lot of records try the following. I'd call this clunky but it may suit you?

In form shown or load event

dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToFirstHeader;
dataGridView1.CellFormatting += DataGridView1OnCellFormatting;

Then add this code to the form

private void DataGridView1OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    var grid = sender as DataGridView;
    if (!(grid.Rows[e.RowIndex].IsNewRow))
    {
        grid.Rows[e.RowIndex].HeaderCell.Value = Convert.ToString(e.RowIndex + 1);
    }
}

enter image description here

Karen Payne
  • 4,341
  • 2
  • 14
  • 31