winform c# datagridview 添加序号列

1.在datagridview中增加一空列放在最前面

2.添加事件代码:

       private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index+1;
            }
        }

(更改查询结果后序号可自动重新排列!)

你可能感兴趣的:(C#2005)