Hi,
Is there an Excel like row header where I can display row index regardless of sort and filtering? I tried adding a column with row.visibleIndex but then the data would have to be added after sort and it seems a bit awkward. Furthermore I would like the row index column to NOT be part of the grid data so I wouldn't have to filter it out when exporting etc.
Thanks, David
Hi David,
There are a couple of ways you could do this. I would probably use the CellExporting event of the UltraGridRowExporter. This event (and most of the events on the component) passes you the CurrentRowIndex and CurrencyColumnIndex, which are settable.
So one way you could do this would be to handle events like CellExporting and watch for the exporting of the first visible cell in the row, then increment the CurrentColumnIndex by 1. This would have the effect of shifting the entire row over by one column. You could then write anything you want into the current cell, such as a numeric index or e.GridRow.VisibleIndex.
You would have to do the same thing in HeaderExporting, of course, to shift the headers over to match the columns.
Another option would be to add an unbound column to the grid and populate it in the InitializeRow event. You don't want this column to show up on-screen, so you could either hide it or not add it to the actual grid. Then you would use the BeginExport event of the ExcelExporter and modify the layout passed into the event. This layout is a clone of the layout of the on-screen grid. So you could make the hdidden column visible in this layout without affecting the on-screen grid, or even add a new unbound column ot the layout here.
Thanks for the quick response!
I'm not sure that I understand your suggestion though, and to make sure that I know, that you know what I'm looking for, I'll try to ask my question again. Bear with me, English is not my mother tounge.
The circled area is what I'm looking for. A part of the grid but not really a part of the data source. I thought that this sort of thing would be built into UltraGrid as I imagine a lot of people need it. I'm still hoping that it is and just want to check with you before I build my own...
Hello,
That is exactly what I am trying to accomplish;
"made me think you were exporting the grid to Excel and wanted to export row numbers there"
Can you tell me how to add row numbers to the excel doc?
I'm using the following to add row numbers to my ultragrid. but the excel isn't exporting w/row numbers:
e.Layout.Bands(0).Override.RowSelectorNumberStyle = RowSelectorNumberStyle.RowIndex e.Layout.Bands(0).Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True e.Layout.RowSelectorImages.ActiveAndDataChangedImage = Nothing Me.Grid.DisplayLayout.Override.RowSelectorHeaderStyle = RowSelectorHeaderStyle.SeparateElement
If you read the third post down from the top, I explained a couple of possible methods for doing that. Personally, I'd probably go with the unbound column approach.
I've attached a quick sample for you here to demonstrate how it's done.
Thank you for the solution. It worked great. Only thing is, the excel row number is coming over as text instead of an integer so in excel you get the "[number stored as text]" warning on the cells.
I ended up adding a column to the "export started" event...
' Add new row count column
e.Layout.Override.AllowAddNew = AllowAddNew.Yes
e.Layout.Bands(0).Columns.Insert(0, "cnt").Header.Caption = "[row #]"
e.Layout.Bands(0).Columns(0).Width = 40
And then on "cell exported", increment each row value..
' Add row #
If excelCell.Value Is Nothing Then
For Each row As WorksheetRow In e.CurrentWorksheet.Rows
e.CurrentWorksheet.Rows(e.CurrentRowIndex).Cells(e.CurrentColumnIndex).CellFormat.Alignment = HorizontalCellAlignment.Center
excelCell.Value = i
i += 1
Next
End If
Sorry about that. I specifically set the DataType on the column to Integer so that would not happen. But apparently, the InitializeRow was triggered by the adding of the column (before I set the DataType). So when InitializeRow sets the Value on the cell to an integer, it got converted into a string, which is the default DataType of the column.
So you can get around that by not using InitializeRow and just looping through the cells after the DataType on the column is established.
I've attached an updated sample here in case you are interested. But there's nothing wrong with your approach using CellExported, either.