datagridview 很好地显示了数据,但我想按顺序查看数字而不是行标题处的指针。
如图所示,标题单元格中有一个箭头。我不想这样,当我编写下面的代码时,只有当鼠标悬停在数字上时才会出现数字。
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
}
我想直接在行标题中看到它,如下图所示
您可以自己绘制行标题:
private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// Draw only if the cell is the row header: e.ColumnIndex == -1
// and the row is not the column header: e.RowIndex >= 0
if (e.ColumnIndex == -1 && e.RowIndex >= 0) {
bool isSelected = (e.State & DataGridViewElementStates.Selected) != 0;
e.PaintBackground(e.ClipBounds, isSelected);
if (isSelected) {
e.PaintContent(e.ClipBounds); // Paints the selection arrow
}
var grid = (DataGridView)sender;
var point = new PointF(15, e.CellBounds.Top + e.CellBounds.Height / 2 - 8);
e.Graphics!.DrawString($"{e.RowIndex + 1}", grid.Font, Brushes.Black, point);
e.Handled = true;
}
}