How to Enumerate rows in DataGridView in c #

0

I have a question about how to fill the rows in a datagridView in c #, I did this but I want it to be shown automatically without having to add a column

private void dataGridViewCatalogoPartesTotal_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
    {
        if (e.RowIndex >= 0 && e.ColumnIndex == Columna.Index)
        {
            e.Value = e.RowIndex + 1;
        }
    }

And this was the result

But I want to list it this way

    
asked by Daniel 03.10.2017 в 18:40
source

2 answers

0

To show the row number in the header of the row, we could use the RowPostPaint event of the DataGridView control.

And then what you already have is applied, but this time instead of drawing it in a new column, it generates it in the default datagrid.

this.dgvUserDetails.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.dgvUserDetails_RowPostPaint);

    private void dgvUserDetails_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
    {
            using (SolidBrush b = new SolidBrush(dgvUserDetails.RowHeadersDefaultCellStyle.ForeColor))
            {
                  e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
            }
    }
    
answered by 03.10.2017 / 18:54
source
0

In the RowEnter event, place this snippet of code.

private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
    this.dataGridView1.Rows[e.RowIndex].HeaderCell.Value = (e.RowIndex + 1).ToString();
}

Greetings.

    
answered by 03.10.2017 в 18:56