How to list the paginated records in DataGrid control and how to list items in the ListBox control?

SheoNarayan
Posted by in Silverlight category on for Beginner level | Points: 250 | Views : 4110 red flag

In this article, we are going to learn about DataGrid and Listbox controls in Silverlight.

DataGrid

DataGrid control is one of the most important control in Silverlight to develop Line-of-Business (As per Wiki - Line of business (LOB) is a general term which often refers to a set of one or more highly related products which service a particular customer transaction or business need.) applications and is mostly used to List the records in tabular format.

Apart from listing the records, it also facilitates to sort, re-arrange columns, resize columns, edit data etc.

This article is the continuation of my last article in Silverlight controls series, read last article here.

Get 500+ ASP.NET web development Tips & Tricks and ASP.NET Online training here.

How to list the paginated records in DataGrid control?

In the scenario, where we need to display large amount of data (spanning more than tens of records) into the DataGrid control its ideal to display paginated data so that user can easily navigate the data. To do this we can use DataPager control.

Code

<Grid x:Name="LayoutRoot" Background="White">

<StackPanel>

<Button x:Name="Button1" Content="FillData" Click="Button1_Click" Height="30" Width="75" Margin="20, 10, 0, 20"/>

<sdk:DataGrid x:Name="DataGrid1" AutoGenerateColumns="True" />

<sdk:DataPager x:Name="DataPager1" PageSize="2" />

</StackPanel>

</Grid>

In the above code snippet, we have a Button, DataGrid and DataPager control. In the DataPager control, we have set PageSize as 2 that force the DataGrid to list only 2 records per page.

Code behind

private void Button1_Click(object sender, RoutedEventArgs e)

{

FillData();

}

private void FillData()

{

IList<Person> list = new List<Person>();

 

Person p = new Person

{

Age = 1,

FirstName = "Joe",

LastName = "Elan",

IsActive = true

};

Person p1 = new Person

{

Age = 15,

FirstName = "Mike",

LastName = "Bull",

IsActive = true

};

Person p11 = new Person

{

Age = 15,

FirstName = "Mike",

LastName = "June",

IsActive = true

};

Person p2 = new Person

{

Age = 51,

FirstName = "Mukul",

LastName = "Alwa",

IsActive = true

};

Person p22 = new Person

{

Age = 51,

FirstName = "Mukta",

LastName = "Bhatia",

IsActive = true

};

Person p3 = new Person

{

Age = 31,

FirstName = "Sheo",

LastName = "Narayan",

IsActive = true

};

list.Add(p);

list.Add(p1);

list.Add(p11);

list.Add(p2);

list.Add(p22);

list.Add(p3);

// use Namespace System.Windows.Data;

PagedCollectionView view = new PagedCollectionView(list);

DataGrid1.ItemsSource = view;

DataPager1.Source = view;

}

public class Person

{

public string FirstName { get; set; }

public string LastName { get; set; }

public int Age { get; set; }

public bool IsActive { get; set; }

}

In the above code snippet, we have set the DataGrid and DataPager controls source to the PagedCollectionView object.

Output

Similarly, we can paginate the ListBox items as well.

ListBox

 

How to list items in the ListBox control?

ListBox control lets us select one or more items from the list. This is derived from System.Windows.Controls.ItemsControl.

Code

<ListBox x:Name="ListBox1" SelectionMode="Multiple" Height="150" Width="150">

<ListBox.Items>

<ListBoxItem Content="First Item" />

<ListBoxItem Content="Second Item" />

<ListBoxItem Content="Third Item" />

<ListBoxItem Content="Fourth Item" />

<ListBoxItem Content="Fifth Item" />

<ListBoxItem Content="Sixth Item" />

<ListBoxItem Content="Seventh Item" />

</ListBox.Items>

</ListBox> 

In the above code snippet, we have set a ListBox control with SelectionMode as Multiple that lets user select multiple items from the ListBox by clicking on each item. SelectionMode as Single allows user to select only one item.

Output

Hope this article was useful. Thanks for reading, hope you liked it.

Keep a watch on forth coming articles on Silverlight. To read my series of articles,click here.

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)