Sr. No/Row Number for Silverlight 3 DataGrid

Hello All,


Let me first wish you all A Very Happy New Year!!!!


Few days back a query was posted on silverlight forum which says:


"I want to add a column in my datagrid control to show Serial Numbers. I want that column to be Constant i-e i dont want to make that serial number field a part of the objects in the collection binded to the ItemSource property of the Datagrid. On the other hand i would prefer a solution other than ValueConverter. Does datagrid itself gives some kind of support for this?? Or in what way it can be done through handling some event of datagrid in code behind??? "


Though I posted the reply, I was not satisfied with it as it was giving problem when Datagrid has paging.


Here is that reply to post: http://forums.silverlight.net/forums/t/156575.aspx


The problem here was when we give paging to datagrid, on each Page it was re-numbering from 1 instead of continuing with the numbers that ended on the previous page. After spending some time on it I could figure it out how make it work.
Here is the correct way:


Xaml Page:


<StackPanel Orientation="Vertical">
<Data:DataGrid x:Name="dgEmployee" SelectionMode="Single" Margin="5" AutoGenerateColumns="False"

Grid.Row="0" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False">
<Data:DataGrid.Columns>
<Data:DataGridTemplateColumn Header="Sr. No" Width="90">
<Data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="tbID"></TextBlock>
</DataTemplate>
</Data:DataGridTemplateColumn.CellTemplate>
</Data:DataGridTemplateColumn>

<Data:DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName" IsReadOnly="True" Width="90">
</Data:DataGridTextColumn>
<Data:DataGridTextColumn Binding="{Binding LastName}" Header="LastName" IsReadOnly="True" Width="90">
</Data:DataGridTextColumn>
</Data:DataGrid.Columns>
</Data:DataGrid>
<Data:DataPager x:Name="customerPager" PageSize="5" Source="{Binding Path=ItemsSource, ElementName=dgEmployee}"></Data:DataPager>
</StackPanel>


Code Behind:
#region Variables
int PageIndex = 0;

#endregion


#region Page Constructor
public MainPage()
{
        InitializeComponent();
        AttachEventHandlers();
        PagedCollectionView pagedcollectionview = new PagedCollectionView(Employee.GetSampleEmployeeList());
        dgEmployee.ItemsSource = pagedcollectionview;
        dgEmployee.Columns[0].IsReadOnly = true;
}
#endregion


#region Attach Event Handlers Method
void AttachEventHandlers()
{
       dgEmployee.LoadingRow += new EventHandler<DataGridRowEventArgs>(dgEmployee_LoadingRow);
}
#endregion


#region DataGrid Events
void dgEmployee_LoadingRow(object sender, DataGridRowEventArgs e)
{
        DataGridRow dgRow = DataGridRow.GetRowContainingElement(e.Row);
        TextBlock textblock = dgEmployee.Columns[0].GetCellContent(dgRow) as TextBlock;
        if (textblock != null)
        {
                if (e.Row.GetIndex() == 0)
               {
                        textblock.Text = ((customerPager.PageSize * customerPager.PageIndex) + 1).ToString();
                        PageIndex = (customerPager.PageSize * customerPager.PageIndex) + 1;
               }
               else
                        textblock.Text = (++PageIndex).ToString();
        }
}
#endregion


Comments are always welcome.


You can download SourceCode.


Happy Coding :)

2 comments:

  1. I think you need to somehow renumber when a row is removed

    ReplyDelete
  2. Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. Website

    ReplyDelete