tags:

views:

44

answers:

2

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..Also when i scroll the page, when the row goes out of visibility and when u scroll back again its number is changing..for example on page load you see 1 to 30 in order when u scroll up and down twice, the order can become 1,30,25,4,...etc..theres no specific order..it displays random number in row header.. 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;
}
A: 

Try setting your column names once, at the right time. E.g. after setting the data source.

thelost
column's are dynamicaly generated.user have control over the columns..so that may not work for me..
prem
can't you track the attempt to change the data source ? or switching column order ?
thelost
i am not getting the point..can you explain me clearly..sorry
prem
can't you just track down the attempt to filter / sort rows and set the row headers properly ? maybe by a hit test.
thelost
no, it doesnot work that way.. i tried it
prem
A: 

I'm doing this in VB.Net. Store the original result set row number in Row(x).Column(0).Tag and then:

Private Sub gridData_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles gridData.RowPostPaint

    Dim style As DataGridViewCellStyle
    Dim Pen As Pen
    Dim rec As RectangleF
    Dim format As StringFormat
    'Dim n As Integer

    'get style to use for row header cells            
    style = gridData.RowHeadersDefaultCellStyle

    'get pen (needed to get text brush)            
    Pen = New Pen(style.ForeColor)

    'get row rectangle and adjust to width of row header            
    rec = e.RowBounds
    rec.Width = gridData.RowHeadersWidth

    'create formating object to center row number            
    format = StringFormat.GenericTypographic
    Format.Alignment = StringAlignment.Center
        Format.LineAlignment = StringAlignment.Center

        'draw the row number in row header            
        e.Graphics.DrawString(gridData.Rows.Item(e.RowIndex).Cells(0).Tag.ToString(), style.Font, Pen.Brush, rec, format)

    End Sub

My code is adapted from a c# example found here: http://www.daniweb.com/forums/thread302079.html

Daniel P