1

atm 我正在尝试编写一个代码,它将用 MySQL 中的数据填充我的 DataGridView

string sql2Params = "Database=" + database + ";Data Source=" + host + ";User Id=" + user + ";Password=" + pass;
        string sql2Query = "SELECT * FROM `wagons` WHERE 1";

        //MySqlConnection sql2Connection = new MySqlConnection(sql2Params);
        //MySqlCommand sql2Command = new MySqlCommand(sql2Query, sql2Connection);

        using (MySqlConnection connection = new MySqlConnection(sql2Params))
        {
            connection.Open();
            using (MySqlCommand cmdSel = new MySqlCommand(sql2Query, connection))
            {
                DataTable dt = new DataTable();
                MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);
                da.Fill(dt);
                wagonDataGrid.DataContext = dt;
            }
            connection.Close();
        }

我找到了这个,然后在不同的网站上检查它是正确的,并试图在我自己的项目中使用,但我得到了错误

Error   1   'System.Windows.Forms.DataGridView' does not contain a definition for 'DataContext' and no extension method 'DataContext' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?)

这可以是什么?

4

1 回答 1

0

使用DataSource而不是DataContext,winform datagrid中没有属性DataContext

于 2012-08-02T08:42:00.667 回答