How to Display line number in the datagrid ?

  • Thread starter Thread starter BlueFox
  • Start date Start date
B

BlueFox

Hi :
When i display the DataBase in System.Windows.Forms. DataGrid ,i want to
display the line number of ever line .How can i do it ?
Any help may be Good .
 
Hi,

You can probably do so by implementing a custom DataGridColumnStyle with
overridden methods responsible for retrieving data from and updating data in
the bound datasource. The retrieval method will simply return the passed row
number, and the update method will not obviously update anything.

An alternative way could be doing custom drawing on row headers by
post-processing the grid's Paint event.
 
It is very easy for the Data Grid Control there is an event called
"ItemDataBound"

Register a function for that event.

In the body of that function write the following code

if(e.Item.ItemType != ListItemType.Header)
e.Item.Cells[0].Text = "" + (e.Item.DataSetIndex + 1);

That's it. You will get line numbers.

regards,
Abhishek.
 
I didn¡¯t describe what I want very well.What I really want is to display
the line number of every line before the first column of DataGrid.They will
do not change even if user sort the datatable by click the DataGrid¡¯s
header.

My English is poor,I can¡¯t describe my mean exactly.

Thank you for you help.



Dmitriy Lapshin said:
Hi,

You can probably do so by implementing a custom DataGridColumnStyle with
overridden methods responsible for retrieving data from and updating data in
the bound datasource. The retrieval method will simply return the passed row
number, and the update method will not obviously update anything.

An alternative way could be doing custom drawing on row headers by
post-processing the grid's Paint event.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

BlueFox said:
Hi :
When i display the DataBase in System.Windows.Forms. DataGrid ,i
want
to
display the line number of ever line .How can i do it ?
Any help may be Good .
 
You should override the OnPaint method then and render row numbers on row
headers, hence you should also ensure the row headers are visible by setting
the appropriate property(ies) in the inherited grid's constructor.
 
I will try it.
Dmitriy Lapshin said:
You should override the OnPaint method then and render row numbers on row
headers, hence you should also ensure the row headers are visible by setting
the appropriate property(ies) in the inherited grid's constructor.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

BlueFox said:
I didn¡¯t describe what I want very well.What I really want is to display
the line number of every line before the first column of DataGrid.They will
do not change even if user sort the datatable by click the DataGrid¡¯s
header.

My English is poor,I can¡¯t describe my mean exactly.

Thank you for you help.
 
Back
Top