C#dataGridView没有DataContext

时间:2012-08-02 08:39:32

标签: c# datagridview datacontext

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?)

这可能是什么?

1 个答案:

答案 0 :(得分:0)

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