【问题标题】:Fill dataGrid from MySQL database in C# WPF在 C# WPF 中从 MySQL 数据库中填充 dataGrid
【发布时间】:2011-09-09 10:48:56
【问题描述】:

我想在我的 WPF 应用程序中填充一个 dataGrid。

我的 XAML:

<DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" 
Margin="102,72,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="848" />

我的代码:

  public void FillGrid()
    {
        string MyConString =    
        "SERVER=myserver.com;" +
        "DATABASE=mydatabase;" +
        "UID=myuserid;" +
        "PASSWORD=mypass;";

        string sql = "SELECT clientnr, name, address FROM clients ORDER BY name";

        MySqlConnection connection = new MySqlConnection(MyConString);
        MySqlCommand cmdSel = new MySqlCommand(sql, connection);
        DataTable dt = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);
        da.Fill(dt);
        dataGrid1.DataContext = dt;
    }

我确定 MySQL 部分是正确的,它没有给出任何错误。 VS10 express 没有给出任何错误。但如果我执行该方法,我的 dataGrid 将不会被填充。

我做错了什么?

提前致谢!

【问题讨论】:

    标签: c# mysql wpf database datagrid


    【解决方案1】:

    我总是使用此代码在我的 Datagrid 中显示/填充数据:

    SqlConnection con = new SqlConnection("your connection string");
        
    private void LoadGrid()
        {
            SqlCommand cmd = new SqlCommand("Select * From XXX;", con);
            DataTable dt = new DataTable();
            con.Open();
            SqlDataAdapter sdr = new SqlDataAdapter(cmd);
            sdr.Fill(dt);
            dataGrid.ItemsSource = dt.DefaultView;
            con.Close();
        }
    

    【讨论】:

      【解决方案2】:

      替换

      dataGrid1.DataContext = dt; 
      

      dataGrid1.ItemsSource = dt.DefaultView;
      

      【讨论】:

      • 这应该解释了为什么建议更换。
      【解决方案3】:

      只需在后面的代码中在InitializeComponents() 之后调用方法FillGrid()。我只是这样做了,它运行完美

      【讨论】:

      • 详情请见
      【解决方案4】:

      您肯定希望它绑定到 DataTable 而不是 Adapter,正如 Rachel 建议的那样(适配器的工作是填充 DataTable)。此外,最好将连接和命令包含在 usings 中,以确保所有内容都已清理干净,如下所示:

      public void FillGrid()
      {
          string MyConString =
          "SERVER=myserver.com;" +
          "DATABASE=mydatabase;" +
          "UID=myuserid;" +
          "PASSWORD=mypass;";
      
          string sql = "SELECT clientnr, name, address FROM clients ORDER BY name";
      
          using (MySqlConnection connection = new MySqlConnection(MyConString))
          {
              connection.Open();
              using (MySqlCommand cmdSel = new MySqlCommand(sql, connection))
              {
                  DataTable dt = new DataTable();
                  MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);
                  da.Fill(dt);
                  dataGrid1.DataContext = dt;
              }
              connection.Close();
          }
      }
      

      【讨论】:

        【解决方案5】:

        设置 DataGrid 的绑定:

        <DataGrid ItemsSource="{Binding }" />
        

        【讨论】:

        • @Lars 查看更新以设置 DataGrid 绑定。另外,验证您的 FillGrid 方法正在运行。
        • @HaiderKhattak 我建议用相关代码发布一个新问题。最可能的原因是您的DataContext 设置不正确。
        猜你喜欢
        • 2016-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多