问题描述
我想要做什么?
我正在开展项目,即在线股票/股票市场。在哪个客户端可以设置一些订单,并可以在他/她的屏幕上看到细节。为此,我使用WPF + C#并使用datagrid,所以所有产品,费率和所有的细节将以表格形式列在该格子中。数据将从Sql Server Management Studio中获取。此外,我在datagrid中绑定数据。_
我正面对什么问题?
我遇到的问题是在datagrid行已添加但数据未显示在这些行中。例如,如果数据库中有4行,则不会。的行显示在datagrid上,但空行显示:(。
What I'm trying to do?
I'm working on project i.e. Online Stock/Shares Marketing. In which client will able to set some order and can see details on his/her screen. For this I'm using WPF + C# and using datagrid so all products, rates and all of its detail will be listed in that grid in a tabular form. And data will be fetched from Sql Server Management Studio. Moreover, I'm binding data in datagrid.
What Problem I'm Facing?
Problem I'm facing is that in datagrid rows are added but data is not appearing in those rows. For example if there is 4 rows in database, same no. of rows is appearing on datagrid but empty rows are appearing :(.
XAML: -
<DataGrid IsReadOnly="True" x:Name="pa" AutoGenerateColumns="False" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding ProductName}" Width="2*" />
<DataGridTextColumn Header="Price" Binding="{Binding ProductPrice}" Width="2*" />
<DataGridTextColumn Header="Unit" Binding="{Binding ProductUnit}" Width="2*" />
<DataGridTextColumn Header="Stock" Binding="{Binding ProductStock}" Width="2*" />
<DataGridTextColumn Header="Code" Binding="{Binding ProductCode}" Width="2*" />
</DataGrid.Columns>
</DataGrid>
C#: -
static ObservableCollection<ProductAva> collection = new ObservableCollection<ProductAva>();
private string ProductName { get; set; }
private string ProductPrice { get; set; }
private string ProductUnit { get; set; }
private string ProductStock { get; set; }
private string ProductCode { get; set; }
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection("Server=DESKTOP-KE9BBUE;Database=Vegomart;Trusted_Connection=True");
con.Open();
SqlCommand command = new SqlCommand("select * from AddNewItem", con);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
collection.Add(new ProductAva() { ProductName = reader.GetString(1), ProductPrice= Convert.ToString(reader.GetInt32(2)), ProductUnit = reader.GetString(4), ProductStock = Convert.ToString(reader.GetInt32(5)), ProductCode = reader.GetString(3)});
}
pa.ItemsSource = collection;
}
请帮我出来..
推荐答案
嗯,最后我找到了解决方案。
Ahhh, At last I found solution to it.
而不是声明属性私人他们应该公开: -
instead of declaring properties private they should be public:-
private string ProductName { get; set; }
private string ProductPrice { get; set; }
private string ProductUnit { get; set; }
private string ProductStock { get; set; }
更正: -
public string ProductName { get; set; }
public string ProductPrice { get; set; }
public string ProductUnit { get; set; }
public string ProductStock { get; set; }
这篇关于价值不在Datagrid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!