I have a DataSet. I don't know the contents of the set. I just have to display a table of the set in a DataGrid. I'm able to do this using the following code. Just to able to work with it, I have created my own DataSet. CustomerDataProvider is the class I have created that has a method that returns a dummy DataSet.
CustomerDataProvider provider = new CustomerDataProvider();
DataSet ds = new DataSet();
DataTable table = new DataTable();
DataView view = new DataView();
public MainWindow()
{
InitializeComponent();
ds = dataset.GetDataSet();
table = ds.Tables[0];
view = table.AsDataView();
this.DataContext = view;
}
<Grid>
<DataGrid x:Name="dynamicGrid" ItemsSource="{Binding Path=., Mode=TwoWay}" ColumnWidth="*" />
</Grid>
Now, if the DataTable contains a bool value, the DataGrid automatically displays a CheckBox. I want to be able to automatically display a ComboBox if the DataTable contains a List of items. How do I go about achieving this?
Instead of auto-generating columns, set AutoGenerateColumns
to false, you can define the columns and bind with different fields of the DataTable
. You can take DataGridTemplateColumn
and provide the CellTemplate
for the same. See the reference code below:
<DataGrid AutoGenerateColumns="False">
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.