请看下面一段代码
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控件显示出来,但出现上述错误,为什么,应该如何修改?请各位大侠帮帮忙!
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控件显示出来,但出现上述错误,为什么,应该如何修改?请各位大侠帮帮忙!
我创建了以个DataGridView控件
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
这个
dataGridView1.DataSource = ds.DefaultView;
this.bindingSource1.DataSource = ds;
this.bindingSource1.DataMember = myTable;
this.dataGridView1.DataSource = this.bindingSource1;
即可
dataGridView1.DataSource = ds.Tables[0].DefaultView; this.Column1.DataPropertyName = "列名";