我有基于类(id、名称、代码、价格)的列表,我试图将该值添加到数据网格中。问题是,数据网格仍然是空的,我的意思是看起来像这样:
XAML
<DataGrid AutoGenerateColumns="False" Height="275" HorizontalAlignment="Left" Margin="337,51,0,0" Name="dataGridProducts" VerticalAlignment="Top" Width="403" Foreground="#FF803E3E">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Width="30" />
<DataGridTextColumn Header="Nazwa" Width="200" />
<DataGridTextColumn Header="Kod" Width="120" />
<DataGridTextColumn Header="Cena" Width="100" />
</DataGrid.Columns>
</DataGrid>
代码背后:
private void categoryListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SqlConnection SqlConn = new SqlConnect().ConnectToSql();
using (SqlConn)
{
SqlConn.Open();
SqlCommand fillCategories = new SqlCommand("...", SqlConn);
SqlDataReader rdr1;
rdr1 = fillCategories.ExecuteReader();
while (rdr1.Read())
{
Products p = new Products(rdr1.GetInt32(rdr1.GetOrdinal("ID")), rdr1.GetString(rdr1.GetOrdinal("ProductName")), rdr1.GetString(rdr1.GetOrdinal("Barcode")), rdr1.GetString(rdr1.GetOrdinal("Price")));
completeProductList.Add(p);
}
}
dataGridProducts.ItemsSource = completeProductList;
}
有什么解决方法可以解决吗?