[英]Getting Data from SQL Server to populate in DataGrid in C# WPF
我正在尝试从数据库中获取数据以在WPF应用程序的DataGrid中进行填充。 我的数据库已连接到VisualStudio 2012,并且有一个.dml文件。 单击主窗口上的“烟花目录”按钮时,应加载数据。
就我所知,我拥有所有必要的参考资料,并且在构建和调试时没有出现任何错误。 此外,新窗口也会打开,并显示网格,但没有数据。 这是一些代码
FireworkCatalog.xaml
<DataGrid Name="FireworkCatalogGrid" HorizontalAlignment="Left" Margin="32,24,0,0" VerticalAlignment="Top" Height="236" Width="508" SelectionChanged="FireworkCatalogGrid_Window_Loaded" Background="#FF7C7878" BorderBrush="#FFDC3B18">
<DataGrid.Columns>
<DataGridTextColumn Header="Type" Binding="{Binding Path=Type}"/>
<DataGridTextColumn Header="Class" Binding="{Binding Path=Class}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"/>
<DataGridTextColumn Header="description" Binding="{Binding Path=Description}"/>
</DataGrid.Columns>
FireworkCatalog.xaml.cs
private void FireworkCatalogGrid_Window_Loaded(object sender, SelectionChangedEventArgs e)
{
FireworkDataDataContext data = new FireworkDataDataContext();
List <fireworkType> fireworks = (from f in data.fireworkTypes
select f) .ToList();
FireworkCatalogGrid.ItemsSource = fireworks;
}
MainScreen.xaml.cs
private void FwkCatalog_btn_Click(object sender, RoutedEventArgs e)
{
FireworkCatalog catalog = new FireworkCatalog();
catalog.ShowDialog();
}
这是对数据库使用Linq to SQL时自动生成的一些代码,因此您可以看到到目前为止的表名和列。
FireworkData.designer.cs
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.fireworkType")]
public partial class fireworkType
{
private string _Type;
private string _Class;
private string _Name;
private string _Description;
public fireworkType()
{
}
}
我们将不胜感激任何帮助,或者关于如何将数据从我的SQL Server获取到我的WPF的任何其他想法。
编辑。
这是另一组代码,以了解如何分配每一列的数据
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Type", DbType="VarChar(50) NOT NULL", CanBeNull=false)]
public string Type
{
get
{
return this._Type;
}
set
{
if ((this._Type != value))
{
this._Type = value;
}
}
}
fireworkType
类型是否具有任何公共属性? 您需要将Type
, Class
, Name
和Description
公开为公共属性,以便绑定能够获取这些值。
private string _Type;
public string Type
{
get { return _Type; }
}
作为一个单独的问题,如果希望这些内容可编辑,则需要添加设置器。 如果要更新这些值并使它们自动显示在网格中,则需要实现INotifyPropertyChanged
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.