请看下面一段代码
        private void button7_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=I3GD1;Initial Catalog=ZHTSystem;User ID=sa;Password=1234";
            con.Open();
            // 创建数据适配器
 SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM myTable", con);
// 创建一个数据集对象并填充数据,然后将数据显示在DataGrid控件中
DataSet ds = new DataSet("myTable");
da.Fill(ds, "myTable");
dataGrid1.DataSource = ds.DefaultViewManager;
}
错误列表:当前上下文中不存在名称“dataGrid1” 数据库我已经连接好了,我想把查找的数据库表的内容通过DataGrid控件显示出来,但出现上述错误,为什么,应该如何修改?请各位大侠帮帮忙!

解决方案 »

  1.   

    你DATAGRID的名字是叫dataGrid1么?
      

  2.   

    回复#1
    我创建了以个DataGridView控件
     private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
            
            } 
    这个
      

  3.   


    dataGridView1.DataSource = ds.DefaultView;
      

  4.   

    再在你的程序中添加一个bindingsource的控件,
    this.bindingSource1.DataSource = ds;
                            this.bindingSource1.DataMember = myTable;
                            this.dataGridView1.DataSource = this.bindingSource1;
    即可
      

  5.   

    把你拖的dataGridView1名称改成dataGrid1,或者将cs代码中的dataGrid1改成dataGridView1
      

  6.   

    参考一下.//假设 dataGridView1里,有一列,名为Column1
                    dataGridView1.DataSource = ds.Tables[0].DefaultView;                this.Column1.DataPropertyName = "列名";