In this article, we are going to learn about DataGrid in silverlight and its related properties.
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 provides option to sort, re-arrange columns, resize columns, edit data etc.
Next, we have DataGrid control with
At last we have set the
It allows keeping the text field type value in the DataGrid. Sorting of this column is inherited out of the box.
It allows keeping the Boolean field type value in the DataGrid. Sorting of this column is inherited out of the box.
It allows keeping type of data that can’t be kept in above two column type, eg. Images, it also allows keeping custom data along with other field type value in the DataGrid. Sorting of this column is NOT inherited out of the box, you need to write your own logic to support sorting of this column.
We have used
To display the Age data, we have specified
In the FillData method, we have created a Generic list collection and added Person object and then set the ItemSource property of the DataGrid to the generic list collection.
We need to simply create the instance of
To freeze first few columns of the Grid, we specify the FrozenColumnCount property of the DataGrid.
In the DataGrid we have attached the DataTemplate by setting its RowDetailsTemplate property.
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 provides option to sort, re-arrange columns, resize columns, edit data etc.
How to list the records in the DataGrid?
Code
In the above code, we have a Button control; on click of the button we are executing<grid x:name="LayoutRoot" background="White">
<StackPanel></grid>
<Button x:Name="Button1" Content="FillData" Click="Button1_Click" Height="30" Width="75" Margin="20, 10, 0, 20"/></StackPanel>
<sdk:DataGrid x:Name="DataGrid1" AutoGenerateColumns="True" />
Button1_Click
server side method.Next, we have DataGrid control with
AutoGenerateColumns
attribute set to true that automatically generates the columns based on the properties found in the data source.
Code behindprivate void Button1_Click(object sender, RoutedEventArgs e)In the above code snippet, in the
{
FillData();}
private void FillData()
{
IList<Person> list = new List<Person>();}
Person p = new Person
{Age = 1,FirstName = "Joe",};Person p1 = new Person
LastName = "Elan",
IsActive = true
{Age = 15,FirstName = "Mike",};Person p2 = new Person
LastName = "Bull",
IsActive = true
{Age = 51,FirstName = "Mukul",};Person p3 = new Person
LastName = "Alwa",
IsActive = true
{Age = 31,FirstName = "Sheo",};
LastName = "Narayan",
IsActive = true
list.Add(p);
list.Add(p1);
list.Add(p2);
list.Add(p3);
public class Person
{public string FirstName { get; set; }}
public string LastName { get; set; }
public int Age { get; set; }
public bool IsActive { get; set; }
Button1_Click
method we are calling FillData
method. In the FillData
method, we have instantiated the Person class and set its value and added to the Generic collection.At last we have set the
ItemsSource
of the DataGrid to the generic list collection.Output
AutoGeneratedColumns
of the DataGrid
allows automatic sorting, resizing and re-ordering of the columns.How to customize the DataGrid’s columns data?
DataGrid has several column types to customize the look and feel of the DataGrid columns.DataGridTextColumn
It allows keeping the text field type value in the DataGrid. Sorting of this column is inherited out of the box.
DataGridCheckBoxColumn
It allows keeping the Boolean field type value in the DataGrid. Sorting of this column is inherited out of the box.
DataGridTemplateColumn
It allows keeping type of data that can’t be kept in above two column type, eg. Images, it also allows keeping custom data along with other field type value in the DataGrid. Sorting of this column is NOT inherited out of the box, you need to write your own logic to support sorting of this column.
Code<sdk:datagrid x:name="DataGrid1" autogeneratecolumns="False" margin="10 10 10 10">In the above code snippet, we have set DataGrid’s
<sdk:DataGrid.Columns></sdk:datagrid>
<sdk:DataGridTemplateColumn Header="Full Name"></sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn.CellTemplate><sdk:DataGridCheckBoxColumn Header="Is Active?" Binding="{Binding IsActive}"/>
<DataTemplate>
<StackPanel Orientation="Horizontal"></DataTemplate>
<TextBlock Text="{Binding FirstName}" x:Name="t1"/></StackPanel>
<TextBlock Text="{Binding LastName}" x:Name="t2"/></sdk:DataGridTemplateColumn.CellTemplate></sdk:DataGridTemplateColumn><sdk:DataGridTextColumn Header="Age" Binding="{Binding Age}"/>
AutoGenerateColumns
property to false so its column will not be automatically generated based on the properties of the DataSource.We have used
DataGridTemplateColumn
with its CellTemplate
that let us specify the template for the data in read only mode. We
will see how to specify template for edit mode later on. In the CellTemplate
, we have specified the DataTemplate
for FirstName and LastName (TextBlocks in the StackPanel, you can’t keep TextBlock directly inside the DataTemplate.
To display the Age data, we have specified
DataGridTextColumn
and to display Active data we have specified DataGridCheckBoxColumn
column type.
Code behindpublic DataGridCustomColumn()In the above code snippet, we have attached
{
InitializeComponent();}
this.Loaded += new RoutedEventHandler(DataGridCustomColumn_Loaded);
void DataGridCustomColumn_Loaded(object sender, RoutedEventArgs e)
{
FillData();}
private void FillData()
{
IList<Person> list = new List<Person>();}
Person p = new Person
{Age = 1,FirstName = "Joe",};Person p1 = new Person
LastName = "Elan",
IsActive = true
{Age = 15,FirstName = "Mike",};Person p2 = new Person
LastName = "Bull",
IsActive = true
{Age = 51,FirstName = "Mukul",};Person p3 = new Person
LastName = "Alwa",
IsActive = true
{Age = 31,FirstName = "Sheo",};
LastName = "Narayan",
IsActive = true
list.Add(p);
list.Add(p1);
list.Add(p2);
list.Add(p3);
DataGrid1.ItemsSource = list;
public class Person
{public string FirstName { get; set; }}
public string LastName { get; set; }
public int Age { get; set; }
public bool IsActive { get; set; }
DataGridCustomColumn_Loaded
method to the Loaded event of the page. In this method we have called FillData
.In the FillData method, we have created a Generic list collection and added Person object and then set the ItemSource property of the DataGrid to the generic list collection.
Output
How to sort the custom template column in DataGrid and Group the records of the DataGrid based on a column value?
How to sort the custom template column in DataGrid?
You must have noticed that when we declare a column as DataGridTemplateColumn in the DataGrid, the default sorting doesn’t work for that column. In case we want to apply sorting on the DataGridTemplateColumn, we need to set SortMemberPath property to a particular column.Code
<sdk:DataGrid x:Name="DataGrid1" AutoGenerateColumns="False" Margin="10 10 10 10">
<sdk:DataGrid.Columns></sdk:DataGrid>
<sdk:DataGridTemplateColumn Header="Full Name" SortMemberPath="FirstName"></sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn.CellTemplate></sdk:DataGridTemplateColumn>
<DataTemplate></sdk:DataGridTemplateColumn.CellTemplate>
<StackPanel Orientation="Horizontal"></DataTemplate>
<TextBlock Text="{Binding FirstName}" x:Name="t1"/></StackPanel>
<TextBlock Text="{Binding LastName}" x:Name="t2"/>
<sdk:DataGridTextColumn Header="Age" Binding="{Binding Age}" />
<sdk:DataGridCheckBoxColumn Header="Is Active?" Binding="{Binding IsActive}"/>
Code behind
void DataGridCustomColumn_Loaded(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 p2 = new Person
{
Age = 51,FirstName = "Mukul",
LastName = "Alwa",
IsActive = true
};Person p3 = new Person
{
Age = 31,FirstName = "Sheo",
LastName = "Narayan",
IsActive = true
};
list.Add(p);
list.Add(p1);
list.Add(p2);
list.Add(p3);
DataGrid1.ItemsSource = list;
}public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public bool IsActive { get; set; }
}
{
FillData();}
private void FillData()
{
IList<Person> list = new List<Person>();
Person p = new Person
{
Age = 1,FirstName = "Joe",};Person p1 = new Person
LastName = "Elan",
IsActive = true
{
Age = 15,FirstName = "Mike",};Person p2 = new Person
LastName = "Bull",
IsActive = true
{
Age = 51,FirstName = "Mukul",};Person p3 = new Person
LastName = "Alwa",
IsActive = true
{
Age = 31,FirstName = "Sheo",};
LastName = "Narayan",
IsActive = true
list.Add(p);
list.Add(p1);
list.Add(p2);
list.Add(p3);
DataGrid1.ItemsSource = list;
}public class Person
{
public string FirstName { get; set; }}
public string LastName { get; set; }
public int Age { get; set; }
public bool IsActive { get; set; }
Output
How to Group the records of the DataGrid based on a column value?
DataGrid records can be grouped based on certain fields data and that can be done usingPagedCollectionView
class that is available in System.Windows.Data namespace
.We need to simply create the instance of
PagedCollectionView
class by passing the source of the data and then add the
PropertyGroupDescription to GroupDescriptions collection of the
PagedCollectionView object.Code
<sdk:DataGrid x:Name="DataGrid1" AutoGenerateColumns="True" />
Code behind
public DataGridGrouping()Once we are done with adding the
{
InitializeComponent();}
this.Loaded += new RoutedEventHandler(DataGridGrouping_Loaded);
void DataGridGrouping_Loaded(object sender, RoutedEventArgs e)
{
FillData();}
private void FillData()
{
IList<Person> list = new List<Person>();}
Person p = new Person
{Age = 1,FirstName = "Joe",};Person p1 = new Person
LastName = "Elan",
IsActive = true
{Age = 15,FirstName = "Mike",};Person p11 = new Person
LastName = "Bull",
IsActive = true
{Age = 15,FirstName = "Mike",};Person p2 = new Person
LastName = "June",
IsActive = true
{Age = 51,FirstName = "Mukul",};Person p22 = new Person
LastName = "Alwa",
IsActive = true
{Age = 51,FirstName = "Mukta",};Person p3 = new Person
LastName = "Bhatia",
IsActive = true
{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);
view.GroupDescriptions.Add(new PropertyGroupDescription("Age"));
DataGrid1.ItemsSource = view;
public class Person
{public string FirstName { get; set; }}
public string LastName { get; set; }
public int Age { get; set; }
public bool IsActive { get; set; }
PropertyGroupDescription
to the GroupDescriptions collection of PagedCollectionView
object, we need to set the ItemsSource of the DataGrid to this collection.Output
How to Freeze columns of the Silverlight DataGrid and how to display selected row details in the DataGrid?
How to Freeze columns of the Silverlight DataGrid?
In some scenario where we have number of columns appearing into the DataGrid, we might need to freeze few columns of the Grid so that it would be easier for the end user to scroll through rest of the columns and see the details.To freeze first few columns of the Grid, we specify the FrozenColumnCount property of the DataGrid.
Code
<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" FrozenColumnCount="2" />
</StackPanel>
<Button x:Name="Button1" Content="FillData" Click="Button1_Click" Height="30" Width="75" Margin="20, 10, 0, 20"/></StackPanel>
<sdk:DataGrid x:Name="DataGrid1" AutoGenerateColumns="True" FrozenColumnCount="2" />
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",};Person p1 = new Person
LastName = "Elan",
IsActive = true
{Age = 15,FirstName = "Mike",};Person p2 = new Person
LastName = "Bull",
IsActive = true
{Age = 51,FirstName = "Mukul",};Person p3 = new Person
LastName = "Alwa",
IsActive = true
{Age = 31,FirstName = "Sheo",};
LastName = "Narayan",
IsActive = true
list.Add(p);
list.Add(p1);
list.Add(p2);
list.Add(p3);
DataGrid1.ItemsSource = list;
public class Person
{public string FirstName { get; set; }}
public string LastName { get; set; }
public int Age { get; set; }
public bool IsActive { get; set; }
Output
How to display selected row details in the DataGrid?
In some scenario, we need to display complete details of selected records. We can do this by declaring a DataTemplate and attaching that template to the DataGrid.Code
<Grid x:Name="LayoutRoot" Background="White">In the above code snippet, you can see that we have declared a DataTemplate under Grid.Resources that contains the TextBlocks that binds the fields of the Data source.
<Grid.Resources></Grid>
<DataTemplate x:Name="RowDetails"></Grid.Resources>
<StackPanel></DataTemplate>
<TextBlock>First Name:</TextBlock></StackPanel>
<TextBlock Text="{Binding FirstName}"/>
<TextBlock>Last Name:</TextBlock>
<TextBlock Text="{Binding LastName}"/>
<TextBlock>Age:</TextBlock>
<TextBlock Text="{Binding Age}"/>
<TextBlock>IsActive:</TextBlock>
<TextBlock Text="{Binding IsActive}"/>
<sdk:DataGrid x:Name="DataGrid1" RowDetailsTemplate="{StaticResource RowDetails}"
RowDetailsVisibilityMode="VisibleWhenSelected" AutoGenerateColumns="False">
<sdk:DataGrid.Columns></sdk:DataGrid>
<sdk:DataGridTextColumn Binding="{Binding FirstName}"/></sdk:DataGrid.Columns>
In the DataGrid we have attached the DataTemplate by setting its RowDetailsTemplate property.
Code behind
public DataGridRowDetails()
{
InitializeComponent();}
this.Loaded += new RoutedEventHandler(DataGridRowDetails_Loaded);
void DataGridRowDetails_Loaded(object sender, RoutedEventArgs e)
{
this.FillData();}
private void FillData()
{
IList<Person> list = new List<Person>();}
Person p = new Person
{Age = 1,FirstName = "Joe",};Person p1 = new Person
LastName = "Elan",
IsActive = true
{Age = 15,FirstName = "Mike",};Person p2 = new Person
LastName = "Bull",
IsActive = true
{Age = 51,FirstName = "Mukul",};Person p3 = new Person
LastName = "Alwa",
IsActive = true
{Age = 31,FirstName = "Sheo",};
LastName = "Narayan",
IsActive = true
list.Add(p);
list.Add(p1);
list.Add(p2);
list.Add(p3);
DataGrid1.ItemsSource = list;
public class Person
{public string FirstName { get; set; }}
public string LastName { get; set; }
public int Age { get; set; }
public bool IsActive { get; set; }
No comments :
Post a Comment