views:

1212

answers:

2

I just want to add a index column in wpf toolkit datagrid to show row index of each data in datagrid. How?

<dg:DataGrid ItemsSource="{Binding List}"                              
                             SelectionMode="Extended"
                             IsReadOnly="True"
                             AutoGenerateColumns="False"
                             HorizontalAlignment="Left"
                             >
            <dg:DataGrid.Columns>



                **<dg:DataGridTextColumn Header="Row Index"></dg:DataGridTextColumn>**
                <dg:DataGridTextColumn Header="Branch" Binding="{Binding Branch.Id}"></dg:DataGridTextColumn>
                <dg:DataGridTextColumn Header="Count" Binding="{Binding RequestCount}"></dg:DataGridTextColumn>

            </dg:DataGrid.Columns>
        </dg:DataGrid>
+2  A: 

I don't have a code sample right now, but accoring to this post there is "a LoadingRow event that you can attach to. In the event you can set the header each time to the number you want based on the item in the event args."

That is the only way I could find to do it. There does not seem to be an elegant XAML solution.

John Myczek
I am using mvvm, that's why I am searching for xaml solution for this problem. thanks for the answer.
NetSide
+3  A: 

you can use a multibinding and a converter to bind to the item in the row and the parent datagrid. then in the converter you look up the position of the row in the datagrids items.

On this page download the sample WPFDatagridWithRowNumbers.zip

Enjoy!

Aran Mulholland
I have a problem to open the link (sites.google). do you have another link for this sample?
NetSide
i can open it using firefox on my pc and my mac. can you get to the site at all? is it just the zip thats giving you grief?
Aran Mulholland
it was about site but I fixed it. Thanks.
NetSide
It works correctly, thanks... :)
NetSide