Is it possible to show row number in the row header of a DataGridView
?
I\'m trying with this code, but it doesn\'t work:
pri
You can also draw the string dynamically inside the RowPostPaint
event:
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);
}
just enhancing above solution.. so header it self resize its width in order to accommodate lengthy string like 12345
private void advancedDataGridView1_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
};
//get the size of the string
Size textSize = TextRenderer.MeasureText(rowIdx, this.Font);
//if header width lower then string width then resize
if (grid.RowHeadersWidth < textSize.Width + 40)
{
grid.RowHeadersWidth = textSize.Width + 40;
}
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);
}
This work in C#:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int idx = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[idx];
long newNo = idx;
if (!_RowNumberStartFromZero)
newNo += 1;
long oldNo = -1;
if (row.HeaderCell.Value != null)
{
if (IsNumeric(row.HeaderCell.Value))
{
oldNo = System.Convert.ToInt64(row.HeaderCell.Value);
}
}
if (newNo != oldNo)
{
row.HeaderCell.Value = newNo.ToString();
row.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
}
}
Thanks @Gabriel-Perez and @Groo, great idea! In case others want it, here's a version in VB tested in Visual Studio 2012. In my case I wanted the numbers to appear top right aligned in the Row Header.
Private Sub MyDGV_RowPostPaint(sender As Object, _
e As DataGridViewRowPostPaintEventArgs) Handles MyDataGridView.RowPostPaint
' Automatically maintains a Row Header Index Number
' like the Excel row number, independent of sort order
Dim grid As DataGridView = CType(sender, DataGridView)
Dim rowIdx As String = (e.RowIndex + 1).ToString()
Dim rowFont As New System.Drawing.Font("Tahoma", 8.0!, _
System.Drawing.FontStyle.Bold, _
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Dim centerFormat = New StringFormat()
centerFormat.Alignment = StringAlignment.Far
centerFormat.LineAlignment = StringAlignment.Near
Dim headerBounds As Rectangle = New Rectangle(_
e.RowBounds.Left, e.RowBounds.Top, _
grid.RowHeadersWidth, e.RowBounds.Height)
e.Graphics.DrawString(rowIdx, rowFont, SystemBrushes.ControlText, _
headerBounds, centerFormat)
End Sub
You can also get the default font, rowFont = grid.RowHeadersDefaultCellStyle.Font
, but it might not look as good. The screenshot below is using the Tahoma font.
It seems that it doesn't turn it into a string. Try
row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
private void ShowRowNumber(DataGridView dataGridView)
{
dataGridView.RowHeadersWidth = 50;
for (int i = 0; i < dataGridView.Rows.Count; i++)
{
dataGridView.Rows[i].HeaderCell.Value = (i + 1).ToString();
}
}