Show row number in row header of a DataGridView

前端 未结 11 2365
野性不改
野性不改 2020-12-01 06:17

Is it possible to show row number in the row header of a DataGridView?

I\'m trying with this code, but it doesn\'t work:

    pri         


        
11条回答
  •  长情又很酷
    2020-12-01 06:37

    This work in C#:

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        int idx = e.RowIndex;
        DataGridViewRow row = dataGridView1.Rows[idx];
        long newNo = idx;
        if (!_RowNumberStartFromZero)
            newNo += 1;
    
        long oldNo = -1;
        if (row.HeaderCell.Value != null)
        {
            if (IsNumeric(row.HeaderCell.Value))
            {
                oldNo = System.Convert.ToInt64(row.HeaderCell.Value);
            }
        }
    
        if (newNo != oldNo)
        {
            row.HeaderCell.Value = newNo.ToString();
            row.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
        }
    }
    

提交回复
热议问题