TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Dynamic DataGrid in Silverlight
Priya Linge
Sep 16, 2011
9
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
This blog demonstrate how we can create the Dynamic DataGrid in Silverlight.
We can create Dynamic DataGrid and add this DataGrid in Grid control which is on MainPage.xaml.
Step 1
: Create Dynamic DataGrid as bleow and add DataGrid in Grid control.
Define variables on page.
Grid
panelGrid;
DataGrid
dataGridFirst;
DataPager
pager;
public
void
getPendingClientGrid()
{
panelGrid =
new
Grid
();
panelGrid.Name =
"PendingClientGrid"
;
RowDefinition
rowDef1 =
new
RowDefinition
();
RowDefinition
rowDef2 =
new
RowDefinition
();
panelGrid.RowDefinitions.Add(rowDef1);
panelGrid.RowDefinitions.Add(rowDef2);
panelGrid.HorizontalAlignment =
HorizontalAlignment
.Stretch;
panelGrid.VerticalAlignment =
VerticalAlignment
.Top;
dataGridFirst =
new
DataGrid
();
dataGridFirst.Name =
"PendingClientDataGrid"
;
dataGridFirst.AutoGenerateColumns =
true
;
dataGridFirst.HeadersVisibility =
DataGridHeadersVisibility
.Column;
dataGridFirst.HorizontalAlignment =
HorizontalAlignment
.Stretch;
dataGridFirst.VerticalAlignment =
VerticalAlignment
.Center;
dataGridFirst.IsReadOnly =
true
;
dataGridFirst.AutoGeneratingColumn +=
new
EventHandler
<
DataGridAutoGeneratingColumnEventArgs
>(dataGridFirst_AutoGeneratingColumn);
dataGridFirst.LayoutUpdated +=
new
EventHandler
(dataGridFirst_LayoutUpdated);
dataGridFirst.LoadingRow +=
new
EventHandler
<
DataGridRowEventArgs
>(dataGridFirst_LoadingRow);
dataGridFirst.PreparingCellForEdit +=
new
EventHandler
<
DataGridPreparingCellForEditEventArgs
>(dataGridFirst_PreparingCellForEdit);
dataGridFirst.HeadersVisibility =
DataGridHeadersVisibility
.All;
Grid
.SetRow(dataGridFirst, 0);
pager =
new
DataPager
();
Grid
.SetRow(pager, 1);
panelGrid.Children.Add(dataGridFirst);
panelGrid.Children.Add(pager);
this
.PendingClientsGrid.Children.Add(panelGrid);
dataGridFirst.Style = (
Style
)
Application
.Current.Resources[
"newDataGridStyle"
];
dataGridFirst.ColumnHeaderStyle = (
Style
)
Application
.Current.Resources[
"DataGridHeaderGlassEffect"
];
}
Step 2
: We can also define the datagrid styles in code behind as below.
dataGridFirst.Style = (
Style
)
Application
.Current.Resources[
"newDataGridStyle"
];
dataGridFirst.ColumnHeaderStyle = (
Style
)
Application
.Current.Resources[
"DataGridHeaderGlassEffect"
];
Step 3
: Add created datagrid in PendingClientsGrid as below.
<
Grid
x
:
Name
="PendingClientsGrid"
Grid.Row
="2"
Margin
="0,5,0,0"
HorizontalAlignment
="Left" />
panelGrid.Children.Add(dataGridFirst);
panelGrid.Children.Add(pager);
this
.PendingClientsGrid.Children.Add(panelGrid);
Step 4
: Call getPendingClientGrid() in page as below.Make 1 method named getPendingClientdata() in this method we binding data for datagrid.
public
void
getPendingClientdata()
{
dataGridFirst.ItemsSource = e.result;
}
public
PendingClientWindow()
{
InitializeComponent();
getPendingClientGrid();
getPendingClientdata();
}
Step 5
: OutPut look like as below.
Summary
: We can create dynamic datagrid and also applying style for datagrid dynamically.
Dynamic DataGrid in Silverlight
Next Recommended Reading
DataGrid.LoadingRow Event in silverlight