Hello all i am using datagrid and it doesnot have auto numbering. so i used the following method to number in rowheader but the problem is the numbers are repeating and when they sort a column they are rearranging themselves..please suggest a way out for me

In XAML page i called the method in the following way

LoadingRow="dg_expSummary_LoadingRow"

In the backend the code is as follows

private void dg_expSummary_LoadingRow(object sender, DataGridRowEventArgs e)
            {
                e.Row.Header = e.Row.GetIndex() + 1;
            }

I think what is happening is that when you sort the grid only the rows that change index are being loaded; try to invalidate /reload the grid, i think that will do the trick.

Hope it helps

Not just with the sorting, its even changing while scrolling..the row header is taking some random numbers.

I am loading from database but the columns are dynamically generated and user controls it. so loading from database is not what i am looking for..

If you are loading from a database you could use the Row_Numbers() function to add an extra field to your resutls set that numbers the rows. Thenset the Header to that number:

SELECT ColumnA, ColumnB, ColumnC, Row_Number() OVER(ORDER BY ColumnA) AS RowNumber
FROM TableA

Imaginary results set:

ColumnA  ColumnB  ColumnC  RowNumber
A         AB        AC        1
B         BB        BC        2         
C         CB        CC        3
D         DB        DC        4

thats easy thing to do.. but as i told u user controls colums to display.. so all the time row number cannot be a column in grid..also is there a way to pass the row number to be displayed on to row header? irrespective or sorting or scrolling, the row header should be static..just like we have in Excel..

Ok, clearly i'm missing something here. Are you saying that at no point do you use a sql query to retreive the data from the database?

Also, when you say you want them like Excel, do you mean that if you sort the rows you dont want the numbers to be sorted with the rows?
ie:
Before Sort

1 A
2 C
3 B

After Sort

1 A
2 B
3 C

yes, thats my point.. even after sorting the numbering should remain..i wanted to do this from interface end..

I found this link that shows one way to do it.
I adapted it slightly to make it a bit clearer.

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            // get style to use for row header cells
            DataGridViewCellStyle style = dataGridView1.RowHeadersDefaultCellStyle;
            // get pen (needed to get text brush)
            Pen pen = new Pen(style.ForeColor);
            // get row rectangle and adjust to width of row header
            RectangleF rec = (RectangleF)e.RowBounds;
            rec.Width = dataGridView1.RowHeadersWidth;
            // create formating object to center row number
            StringFormat format = StringFormat.GenericTypographic;
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;
            // draw the row number in row header
            e.Graphics.DrawString((e.RowIndex + 1).ToString(), style.Font, pen.Brush, rec, format);
        }

If you need help understanding any of this, just ask.

[Edit] Just noticed the XAML reference in your post. I also found this link that may be more useful. (Don't understand it myself as I have not done any XAML stuff).

Edited 6 Years Ago by nick.crane: n/a

I am using datagrid in WPF. This datagrid acts differently compared to others. The solution you provided can be used to win forms but not wpf form :(

I am using datagrid in WPF. This datagrid acts differently compared to others. The solution you provided can be used to win forms but not wpf form :(

Yes, I know, Sorry. I notices after I posted. :$
Did you notice my edit.
I found a couple of references that might be of more use.
http://www.cflex.net/showFileDetails.cfm?ObjectID=735
http://forums.silverlight.net/forums/t/14664.aspx
http://www.microsoft.com/india/msdn/articles/53.aspx?
[Edit] and another
http://stackoverflow.com/questions
/2041168/how-to-show-row-index-for-wpf-toolkit-datagrid

Edited 6 Years Ago by nick.crane: n/a

Perhaps you could try something like this:

private void AddARow(int i)
{
    DataGridViewRow Arow = new DataGridViewRow();
    Arow.HeaderCell.Value = "R" + i.ToString();
    this.Rows.Add(Arow);
}

If your row number i=4, your row header should display R4

This article has been dead for over six months. Start a new discussion instead.