winform datagridview显示序号
时间: 2023-09-11 08:05:37 浏览: 159
显示datagridview行号
要在WinForm的DataGridView中显示行号,可以通过在DataGridView的RowPostPaint事件中编写代码来实现。具体步骤如下:
1. 在DataGridView的属性窗口中,找到Events(事件)选项,并选择RowPostPaint事件。
2. 在RowPostPaint事件的代码编辑器中,可以使用以下代码来实现显示行号的功能:
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
var dgv = sender as DataGridView;
if (dgv != null)
{
Rectangle rect = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, rect, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
}
3. 以上代码会在每行绘制时,在行号列的单元格中显示当前行的序号。需要注意的是,要确保DataGridView的RowHeadersVisible属性设置为True,以显示行号列。
这样,当你运行应用程序并加载数据到DataGridView中时,每一行都会显示对应的行号。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [C# winform dataGridView 显示序号,行号](https://blog.csdn.net/weixin_33699914/article/details/90130190)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [winform DataGridView 显示序号](https://blog.csdn.net/mh942408056/article/details/7073375)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文