我有一个DataGridView与几个列。没有绑定到它的DataTable。
所有值在开始后插入一次,并保持不变。
用户应该能够按除第一个列外的所有列进行排序。
第一列应该始终显示索引(第一行为1,第二行为2,依此类推)。
实现这一点最简单的方法是什么?
根据Jimi的建议,我实现了这个解决方案:
private StringFormat sfRight = new StringFormat () { Alignment = StringAlignment.Far };
private void ..._CellPainting (object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex >= 0)
{
e.PaintBackground (e.ClipBounds, true);
e.Graphics.DrawString ((e.RowIndex + 1).ToString (), e.CellStyle.Font, Brushes.White, e.CellBounds.X + e.CellBounds.Width - 4, e.CellBounds.Y + 4, sfRight);
e.Handled = true;
}
}