1

I have a listview that is being created from a sql server database, how do you include the row numbers?

            <ListView x:Name="lstName" Height="400" Canvas.Top="55" Width="450">
                <ListView.View>
                    <GridView>                     
                        <GridViewColumn Header="Number" Width="auto" 
                         DisplayMemberBinding="{Binding Path=Id}" />
                        <GridViewColumn Header="FirstName" Width="auto" 
                         DisplayMemberBinding="{Binding Path=FName}" />
                        <GridViewColumn Header="LastName" Width="auto" 
                         DisplayMemberBinding="{Binding Path=LName}" />
                        <GridViewColumn Header="StreetAddress" Width="auto" 
                         DisplayMemberBinding="{Binding Path=Street}" />
                        <GridViewColumn Header="City" Width="auto" 
                         DisplayMemberBinding="{Binding Path=City}" />
                        <GridViewColumn Header="State" Width="auto"
                         DisplayMemberBinding="{Binding Path=State}" />                       
                    </GridView>                        
                </ListView.View>
            </ListView>
KeyboardFriendly
  • 1,798
  • 5
  • 32
  • 42

3 Answers3

2

You could set AlternationCount for the ListView to a value greater than the possible number of rows that can be returned. Then, bind to ListView.AlternationIndex to get a zero-based index for the current item.

torrential coding
  • 1,755
  • 2
  • 24
  • 34
  • Any way to get index number starting from 1? – Biswajit Chopdar Nov 15 '17 at 21:51
  • @Raven You can implement an `IValueConverter` to convert the zero-based index to a value incremented by 1. But, please note the limitation of the `AlternationIndex` approach when virtualization occurs: https://stackoverflow.com/questions/6511180/wpf-itemscontrol-the-current-listitem-index-in-the-itemssource/17962582#17962582 – torrential coding Nov 16 '17 at 21:19
1

I don't see an easy way to do that as WPF philosophy is to bind to data, and then the view should be independent.

But I can see kind of a hack to get it to work.

You bind (OneTime) against a Counter property from your ViewModel, and in the get accessor of this property, you iterate the counter in addition to returning it.

That way, each ListView item will have a proper index.

But be careful with that solution, you won't be able to trace back an index to an item after that. If you need to also do that for whatever reason, you should actually make a IdInTable field in your object and populate it correctly in your ViewModel.

Qortex
  • 7,087
  • 3
  • 42
  • 59
  • Im new to WPF, but now its just a simple text box, button, and listview to get the data! What Im doing is using linq to sql to execute a stored procedure. The textbox is used to enter the parameter. Looking into your suggestion! – KeyboardFriendly Mar 07 '13 at 22:39
  • 1
    then maybe the cleanest way to go is to somehow get your sql request to return a `RowId` field (or you generate it afterwards) and you just bind to that field in your `ListView`. The ++ solution is kind of hacky and not well maintanable. – Qortex Mar 07 '13 at 22:42
  • 1
    I think you will also have to remember to requery the data from the stored procedure after row deletions... to keep the numbers in sync! – failedprogramming Mar 07 '13 at 22:56
0

Its a very old post and I have seen many people asked how we can avoid zero-based index with AlternationCount and AlternationIndex. So for other users like me searching a solution. You can do it with a bit unusual(or you can say weird) workaround but its working for me:

Hide the first item row (Status is Model.Status):

<ListView.ItemContainerStyle>
  <Style TargetType="ListViewItem">
    <Style.Triggers>
      <DataTrigger Binding="{Binding Status}" Value="Status">
        <Setter Property="Visibility" Value="Collapsed" />
      </DataTrigger>
     </Style.Triggers>
 </Style>
 </ListView.ItemContainerStyle>

After that add a dummy record in first row:

FileDetail fd = new FileDetail()
{
  FileName = "File Name",
  Count = "Count",
  Status = "Status",
  FilePath = "File Path"
};
FileList.Add(fd);

Manage your iteration throughout your code accordingly.

One more thing you can do with that 0 row. You can hide the original header and make this 0 row as a header.

<ListView.Resources>
  <Style TargetType="{x:Type GridViewColumnHeader}">
   <Setter Property="Visibility" Value="Collapsed" />
  </Style>
 </ListView.Resources>