C# dataGridView1数据显示问题
来源:百度知道 编辑:UC知道 时间:2024/06/19 04:21:07
窗体6中有一个comboBox1和一个dataGridView1
public Form6()
{
InitializeComponent();
this.comboBox1.Items.AddRange(new object[] { "表1","表2"});
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=表");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlCommandBuilder sbd = new SqlCommandBuilder();
if (comboBox1.SelectedIndex == 0)
{
da = new SqlDataAdapter("SELECT * FROM 表1", conn);
sbd = new SqlCommandBuilder(da);
da.Fill(ds, "表1");
this.dataGridView1.DataSource=ds.Tables[ "表1"];
}
public Form6()
{
InitializeComponent();
this.comboBox1.Items.AddRange(new object[] { "表1","表2"});
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=表");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlCommandBuilder sbd = new SqlCommandBuilder();
if (comboBox1.SelectedIndex == 0)
{
da = new SqlDataAdapter("SELECT * FROM 表1", conn);
sbd = new SqlCommandBuilder(da);
da.Fill(ds, "表1");
this.dataGridView1.DataSource=ds.Tables[ "表1"];
}
dataGridView1.DataSource = ds.Tables["表1 "].DefaultView;
dataGridView1.DataSource = ds.Tables["表2 "].DefaultView;
你给定了数据源后没有绑定。在this.dataGridView1.DataSource=ds.Tables[ "表1"];
和dataGridView1.DataSource = ds.Tables["表2 "];
之后都加上这句:this.dataGridView1.DataBind();
数据库源有问题