SIGN UP MEMBER LOGIN:    
ARTICLE

Row Header in DataGrid in Silverlight 3

Posted by Diptimaya Patra Articles | Silverlight with C# November 24, 2009
DataGrid in Silverlight 3 has Row Header, but by default it is collapsed. We will see how it can be useful to a DataGrid display.
Reader Level:
Download Files:
 


Introduction

DataGrid in Silverlight 3 has Row Header, but by default it is collapsed. We will see how it can be useful to a DataGrid display.

Creating the Silverlight Application

Fire up Visual Studio 2008 and create a Silverlight Application, name it as RowHeaderSample.

1.gif

Add a DataGrid to your application.

Create sample data as follows:

public
class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string Contact { get; set; }
}
public partial class MainPage : UserControl
{
    List<Person> myList;
    public MainPage()
    {
        InitializeComponent();
        myList = new List<Person>() 
        {
            new Person{ FirstName="First Name 1", LastName="Last Name 1", Age=20, Contact="00000000000" },
            new Person{ FirstName="First Name 2", LastName="Last Name 2", Age=21, Contact="11111111111" },
            new Person{ FirstName="First Name 3", LastName="Last Name 3", Age=22, Contact="22222222222" },
            new Person{ FirstName="First Name 4", LastName="Last Name 4", Age=23, Contact="33333333333" },
            new Person{ FirstName="First Name 5", LastName="Last Name 5", Age=24, Contact="44444444444" },
            new Person{ FirstName="First Name 6", LastName="Last Name 6", Age=25, Contact="55555555555" },
            new Person{ FirstName="First Name 7", LastName="Last Name 7", Age=26, Contact="66666666666" },
            new Person{ FirstName="First Name 8", LastName="Last Name 8", Age=27, Contact="77777777777" },
            new Person{ FirstName="First Name 9", LastName="Last Name 0", Age=28, Contact="88888888888" },
            new Person{ FirstName="First Name 0", LastName="Last Name 0", Age=29, Contact="99999999999" },
        };
        dgMyGrid.ItemsSource = myList;
    }

Now run the application to test the data loaded in DataGrid.

2.gif

Now to add the Row Header you need to use the following property:

<
data:DataGrid x:Name="dgMyGrid" HeadersVisibility="All"
                   Height="184" Margin="66,50,293,0" VerticalAlignment="Top" Width="350"/>

Now run your application once again to test your Row Header.

3.gif

You got it right!

Now we will just add some more flavor to it. We would try to add the Row Number in the Row Header as you might have experienced in Microsoft Excel.

Use the following Event Handler:

<
data:DataGrid x:Name="dgMyGrid" HeadersVisibility="All" LoadingRow="dgMyGrid_LoadingRow"
                   Height="184" Margin="66,50,293,0" VerticalAlignment="Top" Width="350"/>

Now in the Handler add the following code.

private
void dgMyGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.Header = e.Row.GetIndex() + 1;
}

That's it. Now run your application and see the Rows as Excel Rows.

4.gif
 
Hope you like this article.

Login to add your contents and source code to this article
share this article :
post comment
 

Let me have a look on this issue.

Thanks
Diptimaya Patra

Posted by Diptimaya Patra Mar 02, 2010

Here is my test DataGrid project http://www.2shared.com/file/11771089/9dbb303e/DataGridTest.html. The difference is I try to add items to ObservableCollection<Person> (have to use it intead of List<Person>) with the help of Button in the main window. And after verticall scrolling a number in rowheader changes its value.

Your example works fine (with static collection of data). 

Posted by Name Name Mar 01, 2010

We may have issue in Loading the row. Let me fix it.

Posted by Diptimaya Patra Feb 28, 2010

Let me see where is the problem and will try to fix it.

Posted by Diptimaya Patra Feb 28, 2010

I tried this example in WPF DataGrid, and found out that it doesn't work properly. The number in rowheader get invalid value after vertical scrolling. I think it happens because a row loaded again. I can't fix it. Sorry for my english.

Posted by Name Name Feb 28, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor