Reputation: 1300
I am trying to insert data from a ComboBox into a DataGrid.
Now with this code snippet below I can already insert a row into my datagrid, but the row is showing completely empty for some reason.
private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using (DataEntities DE = new DataEntities())
{
tblExtra E = (tblExtra)cmbAddExtras.SelectedItem;
List<string> items = new List<string> { E.ItemCode };
items.Where(item => item.ToString() != null).ToList().ForEach(i =>
{
dgAddExtras.Items.Add(i);
});
}
}
I have also tried binding my columns through XAML, but it's with the same issue, it's still showing an empty line in my datagrid.
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ItemID}" Header="id" MinWidth="20" MaxWidth="100"/>
<DataGridTextColumn Binding="{Binding ItemCode}" Header="Code" MinWidth="80" MaxWidth="100"/>
<DataGridTextColumn Binding="{Binding ItemDescription}" Header="Description" MinWidth="80" MaxWidth="100"/>
<DataGridTextColumn Binding="{Binding ItemPrice}" Header="Price" MinWidth="80" MaxWidth="100"/>
<DataGridTextColumn Binding="{Binding ItemSellingPrice}" Header="QTY" MinWidth="80" MaxWidth="100"/>
</DataGrid.Columns>
The Column names I gave the class are exactly the same as the Column names in my table, just in case someone wants to know.
I have no idea why it is doing this. It might be something very simple that I am missing, if so, sorry :P and thanks for the help in advance!
Upvotes: 0
Views: 359
Reputation: 1668
Your code is wrong:
In below line you are adding just a string
in the List
'items
'
List<string> items = new List<string> { E.ItemCode.ToString() };
So how can you expect a string will have all such properties which you are refereeing in your DataGrid
columns?
Write it like this:
private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using (DataEntities DE = new DataEntities())
{
tblExtra E = (tblExtra)cmbAddExtras.SelectedItem;
List<tblExtra> items = new List<tblExtra> { E };
items.Where(item => item != null).ToList().ForEach(i =>
{
dgAddExtras.Items.Add(i);
});
}
}
Upvotes: 1