Friday, June 13, 2008

How to add Serial Number Column in DataGridView

This article explains how to add a Serial Number (Row Index) Column in DataGridView control without adding this column in Physical Database or in DataSet's DataTable.



Level: Beginner

Knowledge Required:
  • DataGridView
  • Data Binding

Description:
We use DataGridView control alot of times in Data Manipulation application. Sometimes we require to have a Serial Number (S.No.) Column in DataGridView Control in such a way that no matter how Sorting/Filtering is done, Serial Number should remain constant i.e. in a Sequence.

One way to accomplish this is to create a Column in DataSet's DataTable in which we can store the Serial Numbers, but this will make our job too complex if sorting/filtering is also done. Because we have to re-check the Serial Number column again and again each time the Sorting/Filtering is performed.

So the better way is to use the DataGridView control's Virtual Mode. Here are the steps that we will do,

1) Bind the DataGridView control to some BindingSource and setup its Columns
2) Add another column (Unbound Column) and make it the first column
3) Set its name = ColumnSNo
4) Set its ReadOnly = True
4) Set the DataGridView control's VirtualMode property to True
5) In CellValueNeeded event use the following code:

Private Sub DataGridView1_CellValueNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles DataGridView1.CellValueNeeded
If e.RowIndex >= 0 AndAlso e.ColumnIndex = Me.ColumnSNo.Index Then
e.Value = e.RowIndex + 1
End If
End Sub
Note that if we don't set the VirtualMode Property to True then CellValueNeeded event wouldn't fire


Summary:
To display the Serial Numbers we have added an unbound column and set the Virtual Mode Property of DataGridView control to True. Then in CellValueNeeded Event we just return the Row Index whose value is required

15 comments:

Anonymous said...

you can do it very easeier man!
make 2 columns in the grid 1 column for serial number and one column for data
now make the serial number column invisible!
every time user click on a row u can access the serial number column ;)
nima@learninweb.com

Arsalan Tamiz said...

Sorry nima, but I could NOT get you properly. The issue here is that we are trying to display the serial number with each row in DataGridView control. So that we don't want to add the Serial Number Column in our Data Source i.e. DataTable or any other.

Anonymous said...

I have a problem!
In VirtualMode of DataGridView, i use checkBox column. But it's checked as the radio button. Help me solve this problem.
thanks!

Arsalan Tamiz said...

To use the checkbox as a radio button??? hmmm... ok this may be useful for others too. I will (inshaALLAH) try to put a post for that. Keep in touch.

Arsalan Tamiz said...

There you go,

Using DataGridView CheckBox Column as RadioButton (OptionButton)

Anonymous said...

Hello friend,

Actually I have same problem of srno to grid. when I search it on google your article helps me a lot.

So thanks for such good posting.

Thanks and Regards,
arjun

Anonymous said...

Hi thanks for the post i want exactly that you provide as example..
thanks for posting this useful article...


Nilesh Jain
MIT Mandsaur

Anonymous said...

Thanks for the solution but I am facing here one problem that if I input into any other cell of the data grid then value is lost on leaving the focus from that cell.
Why It's happening?

Arsalan Tamiz said...

Make sure the column in which you are entering your value must be a bound column

Unbound Column does NOT render its own value in Virtual Mode. It depends on CellValueNeeded event as we are handling the S.No. Column.

Unknown said...

Thank you sooo much...have been googling to get this thing like crazyieeeeeeee since hours...

your post reaaalllllllyyyyy helped..

thank you sooooooooooooooooo much.:)

Demo said...

Not working..........

Anonymous said...

Thankssssssssssssssss aaaaaaaaaaaaaaaaa lottttttttttttt.
Good post, Helpful post

Anonymous said...

Thank you very much and keep on posting such wonderful things, God bless you my friend.

Anonymous said...

Thanks a lot

God Bless you

Anonymous said...

Thanks for the tips.
By the way why my code below is erorr :

datagridview.Columns("No").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

Thanks.