Hi,
I have a request from a client who would like to see a row count or line number for each row in the GridView. I first looked through the grid's properties but I did not see a setting to display a line number or anything.
So I think this could be done by adding an unbound column (the grid has about 15 columns and is bound to a generic List of business objects) and outputting a line number, by initializing the count to 0 and incrementing it as each row is rendered. Grouping is enabled too, but grouped rows should just be treated like a normal data row.
I'm just not quite sure how to go about doing this or what the best way to accomplish this would be. Any help would be greatly appreciated.
Thank you,
Adam
Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.
Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.
Hi Adam;
I suggest the following solution:
protected void ASPxGridView1_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e) { e.Value = e.ListSourceRowIndex; }
Thanks
Kate.
Kate,
Thank you, that works great!
I see the ListSourceRowIndex property is the current row's index in the data source. When grouping is applied, the data source remains the same but the rows are split into the different groups and since the rows are split into the separate groups, they retain their index since no data source change is done.
I am going to check with my client, and looking back at my original post I worded it wrong, but do you know if it is possible to re-apply the line numbering so when grouped the line number would reflect the current position in the group, for each group? Or, re-apply the line numbering so the first record in the first group is "1" and the last record in the last group is "X", where the data source has "X" total records?
Again, thank you very much!
Adam
Hi Adam;
Another solution that I can suggest you is to use the HtmlDataCellPrepared event handler as follows:
protected void ASPxGridView1_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) { if (e.DataColumn.FieldName == "Unbound") { e.Cell.Text = e.VisibleIndex.ToString(); } }
Note that in this situation the current number includes the number of group rows before the current row.
Thanks
Kate.
Hi Kate,
Well my client would like the line numbers to reflect their current position in the grid so I ended up changing the code to output the line number to the HtmlDataCellPrepared event method as you suggested.
However, I was trying to make the index ignore the grouped rows so that they would not be counted. I could not see where to find if the row is a Group row (since the TableDataCellEventArgs object only has the DataColumn property and no Row property) so I did some looking in HtmlRowPrepared. That does have the row type via e.RowType as well as e.VisibleIndex, but I am not sure how to locate my unbound grid column using the TableRowEventArgs object.
I have access to the Cells collection of the row but I am not sure of how to find the cell for the unbound column for that row. If you have any suggestions or could point me in the right direction I would appreciate it. Thank you for all your help thus far.
Adam
Just a quick update, I was do what I needed to ignore the group rows by adding a template to the unbound column and putting a label there. I also added a private class variable (int) and set it equal to 1 in the declaration.
Then in the HtmlRowPrepared event method I get a reference to the Label using the grid.FindRowCellTemplateControl() method and set the Label's .Text property to the class variable, and incremented the class variable for the next row.
This works great, except when changing pages. Since the class variable is set to 1 in the declaration, on callback it gets reset so each page has the same line numbers instead of continuing from page-to-page.
To solve this I handled the PageIndexChanged event and in the method for it, got the PageSize and PageIndex from the grid. I then set the class variable's value to PageSize * PageIndex + 1.