RowPostPaint Event

In this example the grey column which is present by default on the left side of DataGrid, I have filled it by serial number to sometimes know the number of records entered in the DatGrid..


private void datagrdvwtdsoth_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
string strRowNumber = (e.RowIndex + 1).ToString();
while (strRowNumber.Length < datagrdvwtdsoth.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;
SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
if (datagrdvwtdsoth.RowHeadersWidth < (int)(size.Width + 20)) datagrdvwtdsoth.RowHeadersWidth = (int)(size.Width + 20);
Brush b = SystemBrushes.ControlText;
e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
}


Change the DatGrid name to your grid name.. And this would work perfect.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: