How to display serial numbers in Datagridview
Datagirdview is often used component to display records from tables/query in a tabular fashion.However it does have a short comming that it do not have any properties to display serial number for the records.This is a much desired and neccasary feature for the datagridview.
Here are two ways you can display the records in a datagridview with the serial numbers.
- The sql way :
In this we will change the sql used to create dataset and fill the dataadapter.One more column would be added to the column list that will have the serial number equal to row number.And since this is column it can be used just like other column having properties like header-text and others.You have to change your sql a bit.
say your sql statement is" select fieldname1,fieldname2 from tablename where fieldname1 is not null order by fieldname1 "
now change the sql to
"select rowno() over ( order by fieldname1) as slno,fieldname1,fieldname2 from tablename where fieldname1 is not null order by fieldname1 "
Here you have added another column slno which have the value equal to row numbers and order corrosponding to fiedname1.
please note that your sql need not be exactly like my example. It could be any sql except that you can not use all (*) to represent all the field in the table.You have to explicitly mention the column name
in the sql.
Result of using this in an implemention done be me is as follows: -
The rowpostpaint event:
This is another way to achieve the same result albeit you cant specify the headertext for the column but serial number will be elegently display with proper 0 fill for the numbers.Besides you need not bother about sql or changing your codes.Just add a simple event and you are done.
This is an example of this method implemented by me.private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { int RowNumWidth = dataGridView1.RowCount.ToString().Length; StringBuilder RowNumber = new StringBuilder(RowNumWidth); RowNumber.Append(e.RowIndex + 1); while (RowNumber.Length < RowNumWidth) RowNumber.Insert(0, "0"); // get the size of the row number string SizeF Sz = e.Graphics.MeasureString(RowNumber.ToString(), this.Font); // adjust the width of the column that contains the row header cells if (dataGridView1.RowHeadersWidth < (int)(Sz.Width + 20)) dataGridView1.RowHeadersWidth = (int)(Sz.Width + 20); // draw the row number e.Graphics.DrawString( RowNumber.ToString(), this.Font, SystemBrushes.ControlText, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - Sz.Height) / 2)); }just put these codes in the rowpostpaint event of your datagridviews.Change the datagridview name as assigned by you in the entire code.
Result of the above code is as follow:
Hope these tit bits would be of intrest and use to you.
Yours comments are surely appreciated.


I derive strength from myteam,myfamily and GOD.I believe teamwork elevates a task to
perfection and everyone is contented with their contribution to qualify for the sweet
joy of success.I value commitment,dedication and above all a zeal to do something right
and proper.
free trials
18 Apr, 2010
That is nice to definitely find a site where the blogger knows what they are talking about.
free game download
19 Apr, 2010
very well information you write it very
clean. I’m very lucky to get this information from you. Free Download Games
Wholesale polo shirts
22 Apr, 2010
Good share,you article very great, very usefull for us…thank you
phone cards compare
23 Apr, 2010
really appreciate YOU — thanks a lot!
Hermes birkin bag
24 Apr, 2010
This is an interesting article. Thanks for sharing.
lebron james dunks
25 Apr, 2010
Good share,you article very great, very usefull for us…thank you
cheap mlb jerseys
26 Apr, 2010
good share, great article, very usefull for us…thank you
general auto insurance
28 Apr, 2010
Wonderful journey and experience!