Reputation: 71
i´m trying to add a checkboxcolum in a datagrid control in WPF.
I´ve done this in Windows.Forms and it works very well.
So now i want to write my new program in WPF for the future.
My way that i want to do: The Data will come from a Database as a Dataset.
Some fields have values that i want to display as a checkbox. Now i´ve set the datagrid autocreatecolumn true, the data will be displayed.
Now i want to delete the column that displays the value and will add a checkbox column.
Is that possible or should i create the columns via datatemplate?
Upvotes: 0
Views: 771
Reputation: 376
WPF DataGrid provides a feature called AutoGenerateColumns that automatically generates column according to the public properties of your data objects. It generates the following types of columns based on the type of the value, and you dont have to do anything.:
1. TextBox columns for string values
2. CheckBox columns for boolean values
3. ComboBox columns for enumerable values
4. Hyperlink columns for Uri values
Upvotes: 1
Reputation: 3298
You can subscibe to AutoGeneratingColumn
event and change the column that is being generated:
public MyWindow(){
myDataGrid.AutoGeneratingColumn += AutoGeneratingColumnHandler;
}
private void AutoGeneratingColumnHandler(object sender, DataGridAutoGeneratingColumnEventArgs e) {
var bindingPath = ((e.Column as DataGridBoundColumn).Binding as Binding).Path.Path;
if (bindingPath == "MYPATH") {
var checkBoxColumn = new DataGridCheckBoxColumn();
checkBoxColumn.Binding = new Binding(bindingPath);
e.Column = checkBoxColumn;
}
}
Upvotes: 0