How to add rownumbers in datagrid?
-
Thursday, September 29, 2011 6:03 AMI want to display rownumbers not ids in datagrid
All Replies
-
Friday, September 30, 2011 10:45 PM
I want to display rownumbers not ids in datagrid
What are you trying to achieve? -
Wednesday, October 05, 2011 8:06 AM
hi!you can do something like this BUT IT IS NOT PERFECT and maybe there is better way for doing it (im also curious):create localproperty for your screen (ie. "dummybox" - string and not required) and set its display name to ie. "R/N", drag that property to your datagrid row on screen and set its label position under properties to be "left aligned".. then you can do something like this:Private Sub Customers_Loaded(succeeded As Boolean) 'collection of your datagrid Dim index As Integer = 0 For Each item As Customer In Me.Customers Me.FindControlInCollection("DummyBox", Me.Customers(index)).DisplayName = (index + 1).ToString index += 1 Next End Sub
downside of this approach is that you have column with rownumber and ":" sign like "1:", or "2:", in new row you will have "R/N" for label and if you add new item in grid, this will be changed after collection refresh, and when you change sorting, row numbers would also be reorderd.. but maybe you will get better idea how to do that, i believe that you can add custom label control and programmatically change what ever you need/want, but maybe it helps..Kivito- Proposed As Answer by cte00 Tuesday, May 22, 2012 10:27 AM
-
Thursday, October 06, 2011 6:48 AM
hi, i was playing with this and come to little bit better solution combining posts from mr Yann Duran and from silverlight forums:
in screen code you can put:
Private Const ITEMS_CONTROL As String = "grid" 'name of datagrid control on screen Private WithEvents _itemsControl As DataGrid = Nothing Private Sub <Screen_Name>_InitializeDataWorkspace(saveChangesTo As System.Collections.Generic.List(Of Microsoft.LightSwitch.IDataService)) AddHandler Me.FindControl(ITEMS_CONTROL).ControlAvailable, AddressOf DemoItems_ControlAvailable End Sub Private Sub DemoItems_ControlAvailable(send As Object, e As ControlAvailableEventArgs) _itemsControl = TryCast(e.Control, DataGrid) If (_itemsControl Is Nothing) Then Return Dim col As New DataGridTextColumn With col .Binding = New System.Windows.Data.Binding("Dummy") .Binding.Mode = Windows.Data.BindingMode.OneTime .IsReadOnly = True .CanUserSort = False End With _itemsControl.Columns.Insert(0, col) AddHandler _itemsControl.LoadingRow, AddressOf ItemsControl_LoadingRow RemoveHandler Me.FindControl(ITEMS_CONTROL).ControlAvailable, AddressOf DemoItems_ControlAvailable End Sub Private Sub ItemsControl_LoadingRow(sender As Object, e As DataGridRowEventArgs) Dim indx = (e.Row.GetIndex + 1).ToString Dim texttoupdate = TryCast(_itemsControl.Columns(0).GetCellContent(e.Row), TextBlock) texttoupdate.Text = indx End Sub
still it's not perfect, if control is hidden behind tab it does not fire "ControlAvailable" event so there is no "LoadingRow" event, if you have checked option in datagrid "show new row" that row will still have new number and when you adding new items, until refresh it will have same old number (if your new row have number "3", no matter what number of items you add, new row will have that number "3", until screen refresh), and didnt find how to set column header, it does not work when you define new column (col.header = "R/N").. but it seems that this is closer to (a kind of) solution..
Kivito
- Edited by Kivito Thursday, October 06, 2011 6:48 AM
-
Tuesday, November 08, 2011 12:36 PM
If the DataGrid is in a tab you can use the following:
Register an attached dependency property (let's call it RowLoadedListenerProperty) of type of object and implement an OnRowLoadedListenerPropertyChanged static method that should be invoked every time the attached proeprty changes. In this method find (using VisualTreeHelper) parents (of the passed DependencyObject first parameter) of type DataGridRow and DataGrid and set the Text of the TextBlock in the DataGrid's fist column to the index of the DataGridRow + 1.
In the InitializeDataWorkSpace:
- Bind the dependency property (RowLoadedListenerProperty) to the DataContext of a DataRow content item: this.FindControl(some data row content item name).SetBinding(MyScreen.RowLoadedListenerProperty, String.Emtpy).
- As Kivoto suggested: use this.FindControl(DataGrid name).ControlAvailable to insert a DataGridTextColumn as a first column in the DataGrid.
-
Tuesday, November 08, 2011 1:39 PM
It might be possible to make a WCF RIA service that had a calculated property. You would have to agree on the size of each page, and the service would have to take the page number as a parameter, so that it would know that the second record of page 3 with 10 records per page should be record number 32.
I also think you would have to have the WCF RIA service perform the query to get the records, then return an collection using a for each loop.
I have examples of WCF RIA Services here:
http://lightswitchhelpwebsite.com/Blog/tabid/61/tagid/21/WCF-RIA-Service.aspx

