I want to bind datagrid with datatable, so that it could display and update the data back to the database automatically. But meanwhile i also want to customize the columns of the datagrid, for example for one particular column of datatable, i want to display this column's data in combobox inside the datagrid (the changed data through combobox should be stored back to database as well). How can i do this ? I am new to WPF, so any sample regarding it would be helpful for me. Thanks in advance!
1 Answers
4
votes
You need to set the DataGrid's AutoGenerateColumns to false, then specify your own column list. Here's a rough example:
<DataGrid.Columns>
<DataGridTextColumn Header="Date" Binding="{Binding Path=MyDateColumn, StringFormat={}{0:MM/dd/yy hh:mmt}}" />
<DataGridTextColumn Header="Name" Binding="{Binding Path=NameColumn}" />
<DataGridTemplateColumn Header="SomeValue">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{StaticResource ComboBoxItems}"
SelectedItem="{Binding Path=SomeValueColumn}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>