gpt4 book ai didi

c# - 在 DataGridView 的行标题中显示行号

转载 作者:IT王子 更新时间:2023-10-29 03:57:00 26 4
gpt4 key购买 nike

是否可以在 DataGridView 的行标题中显示行号?

我正在尝试使用这段代码,但它不起作用:

    private void setRowNumber(DataGridView dgv)
{
foreach (DataGridViewRow row in dgv.Rows)
{
row.HeaderCell.Value = row.Index + 1;
}
}

我是否必须设置一些 DataGridView 属性?

最佳答案

您还可以在 RowPostPaint 事件中动态绘制字符串:

private void dgGrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
var grid = sender as DataGridView;
var rowIdx = (e.RowIndex + 1).ToString();

var centerFormat = new StringFormat()
{
// right alignment might actually make more sense for numbers
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};

var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
}

关于c# - 在 DataGridView 的行标题中显示行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9581626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com