简体   繁体   English

从SQL Server获取数据以在C#WPF中填充到DataGrid中

[英]Getting Data from SQL Server to populate in DataGrid in C# WPF

I am trying to get the data from my database to populate in my DataGrid in my WPF application. 我正在尝试从数据库中获取数据以在WPF应用程序的DataGrid中进行填充。 I have my database connected to to VisualStudio 2012 and have a .dml file for it. 我的数据库已连接到VisualStudio 2012,并且有一个.dml文件。 The Data should load when you click on the "Firework Catalog" button on the main window. 单击主窗口上的“烟花目录”按钮时,应加载数据。

To my knowledge I have all necessary references and nothing is erroring out when I build and debug. 就我所知,我拥有所有必要的参考资料,并且在构建和调试时没有出现任何错误。 Also the new Window opens, and presents the grid, but no data. 此外,新窗口也会打开,并显示网格,但没有数据。 Here is some code 这是一些代码

FireworkCatalog.xaml 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 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 MainScreen.xaml.cs

 private void FwkCatalog_btn_Click(object sender, RoutedEventArgs e)
  {
       FireworkCatalog catalog = new FireworkCatalog();
       catalog.ShowDialog();
  }

Here is some of the auto generated code when using the Linq to SQL for the database so you can see the table name and the columns thus far. 这是对数据库使用Linq to SQL时自动生成的一些代码,因此您可以看到到目前为止的表名和列。

FireworkData.designer.cs 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()
    {
    }
    }

Any help would be greatly appreciated, or any other ideas on how to get the data from my SQL Server to my WPF. 我们将不胜感激任何帮助,或者关于如何将数据从我的SQL Server获取到我的WPF的任何其他想法。


EDIT. 编辑。

Here is another set of code to see how each column's data can be asscessed 这是另一组代码,以了解如何分配每一列的数据

[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;
            }
        }
    }

Does the fireworkType type have any public properties? fireworkType类型是否具有任何公共属性? You'll need to expose Type , Class , Name , and Description as public properties in order for the binding to be able to pick up the values. 您需要将TypeClassNameDescription公开为公共属性,以便绑定能够获取这些值。

private string _Type;
public string Type
{
    get { return _Type; }        
}

As a separate issue, if you want these to be editable you'll need to add setters. 作为一个单独的问题,如果希望这些内容可编辑,则需要添加设置器。 If you want to update these values and have them display automatically in the grid, you'll need to implement INotifyPropertyChanged . 如果要更新这些值并使它们自动显示在网格中,则需要实现INotifyPropertyChanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM