在 vb.net 中的 datagridview 中添加行号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14230060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add row number in datagridview in vb.net
提问by Brijesh Patel
I am developing Windows Application in Vb.Net. Now there is one form in which I want to print records displayed in the grid. There is a facility to sort grid by clicking on the cell header in the grid And that should be print as displayed in the grid.
我正在 Vb.Net 中开发 Windows 应用程序。现在有一种表格,我想在其中打印网格中显示的记录。通过单击网格中的单元格标题可以对网格进行排序,并且应该按照网格中的显示进行打印。
So I am little bit confuse about how to maintain the row number in the grid. I can take row number from DB initially when grid fill and assign data source. But when user clicks any cell header and sort that column then the row number is changed. At that time it is very difficult for me to maintain the row number.
所以我对如何维护网格中的行号有点困惑。我可以在网格填充和分配数据源时最初从数据库中获取行号。但是当用户单击任何单元格标题并对该列进行排序时,行号就会改变。当时我很难维护行号。
Can any one give me idea how to maintain row number in the grid?
谁能告诉我如何在网格中维护行号?
Thanks in advance.
提前致谢。
回答by Vishal Suthar
I guess you need this:
我想你需要这个:
NOTE: This code is in C# so you can just convert it to VB.Net
注意:此代码在 C# 中,因此您可以将其转换为 VB.Net
delegate:
代表:
this.dgvUserDetails.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.dgvUserDetails_RowPostPaint);
Event:
事件:
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);
}
}
OUTPUT
输出



