rahulinfo.com: Some Helps, some Tips, some Musing

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.

  1. 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:

    Example 1

    Example 1

  2. 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:

    Example 2

    Example 2

  3. Hope these tit bits would be of intrest and use to you.
    Yours comments are surely appreciated.

You can follow any responses to this entry through the RSS 2.0 feed.