Reputation: 1690
I've been driving myself nuts with this and I'm hoping I'm just missing something obvious.
I have a DataGrid
I want to bind to an in-memory DataTable
. With AutoGenerateColumns set to true, I can edit the fields and have the changes propagate to the datatable and everything is great. The problem is that I wanted to have a Drop-down combobox for the 'Status' field. I then set AutoGenerateColumns
to false
and manually added the column headers in XAML. When I do this I can no longer edit the fields and have the changes persist, instead each field is blank after I click away. I checked the status of the Datagrid columns during debugging and found that the bindings are reading as null. Any ideas of what I've done wrong?
XAML Code:
<DataGrid Margin="31,29,0,29" Name="dgTickets" RowEditEnding="dgTickets_RowEditEnding" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID #" x:Name="ID" />
<DataGridTextColumn Header="Customer Name" x:Name="CustomerName" />
<DataGridTextColumn Header="Phone #" x:Name="Phone"/>
<DataGridComboBoxColumn Header="Status" x:Name="Status"/>
<DataGridCheckBoxColumn Header="Bool?" x:Name="bool1"/>
<DataGridCheckBoxColumn Header="Bool?" x:Name="bool2"/>
<DataGridCheckBoxColumn Header="Bool?" x:Name="bool3"/>
<DataGridTextColumn Header="Notes" x:Name="Notes"/>
</DataGrid.Columns>
</DataGrid>
My C# code for creating the DataTable and associating it:
//add columns
column = new DataColumn("ID", System.Type.GetType("System.String")); //two piece constructor - adds header string and data type
dbTable.Columns.Add(column); //adds the column to the table
column = new DataColumn("CustomerName", System.Type.GetType("System.String"));
dbTable.Columns.Add(column);
column = new DataColumn("Phone#", System.Type.GetType("System.String"));
dbTable.Columns.Add(column);
column = new DataColumn("Status", System.Type.GetType("System.String"));
dbTable.Columns.Add(column);
column = new DataColumn("bool1", System.Type.GetType("System.Boolean"));
dbTable.Columns.Add(column);
column = new DataColumn("bool2", System.Type.GetType("System.Boolean"));
dbTable.Columns.Add(column);
column = new DataColumn("bool3", System.Type.GetType("System.Boolean"));
dbTable.Columns.Add(column);
column = new DataColumn("Notes", System.Type.GetType("System.String"));
dbTable.Columns.Add(column);
//add rows
row = dbTable.NewRow();
dbTable.Rows.Add(row);
dbTable.AcceptChanges();
dgTickets.ItemsSource = dbTable.DefaultView;
Any advice is appreciated. I've tried several different ways to get these two elements to work together, but I can't seem to get two to bind properly if the headers are manually defined.
Upvotes: 0
Views: 1462
Reputation: 102753
I think you just need to throw the bindings in there, eg:
<DataGridTextColumn Header="Customer Name" x:Name="CustomerName" Binding={Binding path=CustomerName} />
Upvotes: 1