Unable to add a column which contains a Button to a dynamic DataGrid
I am working on an application to creating dynamic datagrids based on the tables in returned dataset.
All tables may have different columns so i creating dynamic datagrid on basis of row count and set AutoGenerateColumns = true; Here is my C# code: for (int count = 0; count < ds.Tables.Count; count++)
{
DataGrid dg = new DataGrid();
dg.Name = ds.Tables[count].TableName.ToString();
dg.Margin = new Thickness(5);
dg.Width = 800;
dg.MaxHeight = 200;
dg.AutoGenerateColumns = true;
dg.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
dg.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
dg.ItemsSource = ds.Tables[count].DefaultView;
stkCollection.Children.Add(dg);
}
I want to add an extra column with every dynamic datagrid which have a button in every row.
Here is my XAML:
<GroupBox Header="Log Details">
<Border >
<Grid Background="{StaticResource NormalBackground}">
<Grid.ColumnDefinitions >
<ColumnDefinition Width="150" />
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="60"/>
<RowDefinition Height="10"/>
<RowDefinition Height="*" MaxHeight="600"/>
</Grid.RowDefinitions>
<Button Name="btnMessage" Content="Mess开发者_开发技巧age" Grid.Row="1" Grid.Column="2" Width="120" Height="50" HorizontalAlignment="right" Click="btnMessage_Click"></Button>
<Button Name="btnDraw" Content="Draw" Grid.Row="1" Grid.Column="4" Width="120" Height="50" HorizontalAlignment="right" Click="btnDraw_Click"></Button>
<StackPanel x:Name="stkCollection" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="8" Orientation="Vertical"/>
</Grid>
</Border>
</GroupBox>
Any one have any idea.
How i do this? Thanks in advanceYou can define a DataGridTemplateColumn
as a resource in Xaml like this:
<Window.Resources>
<DataGridTemplateColumn x:Key="DGTemplateColumn">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Info" Click="DGCell_Button_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</Window.Resources>
Add this to your DataGrid in code:
DataGrid.Columns.Add(FindResource("DGTemplateColumn") as DataGridTemplateColumn);
And handle the click event of the button (here i display the name of an employee using the Buttons's DataContext which is the underlying object that is being displayed in that row):
private void DGCell_Button_Click(object sender, RoutedEventArgs e)
{
Employee emp = (sender as Button).DataContext as Employee;
MessageBox.Show(emp.Name);
}
Thanks for quick reply.. I found the solution... Here is my code...
for (int count = 0; count < ds.Tables.Count; count++)
{
DataGrid dg = new DataGrid();
dg.Name = ds.Tables[count].TableName.ToString();
dg.Margin = new Thickness(5);
dg.Width = 800;
dg.MaxHeight = 200;
DataGridTemplateColumn dgc = new DataGridTemplateColumn();
DataTemplate dtm = new DataTemplate();
FrameworkElementFactory btnReset = new FrameworkElementFactory(typeof(Button));
btnReset.SetValue(Button.ContentProperty, "Restore");
btnReset.SetValue(Button.ToolTipProperty, "Restore Selected Row");
btnReset.SetValue(Button.DataContextProperty, new Binding("TableName"));
btnReset.AddHandler(Button.ClickEvent, new RoutedEventHandler(btn_Click));
//set the visual tree of the data template
dtm.VisualTree = btnReset;
dgc.CellTemplate = dtm;
dg.Columns.Add(dgc);
dg.AutoGenerateColumns = true;
dg.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
dg.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
dg.ItemsSource = ds.Tables[count].DefaultView;
stkCollection.Children.Add(dg);
}
Thanks..
精彩评论