
将combobox中的数据显示到dataGridView
我现在已经将数据库和表绑定到COMBOBOX中了现在想通过这个BUTTON确认按钮实现将COMBOBOX选中表中的内容显示到datagirdview中,该怎么在butto...
我现在已经将数据库和表绑定到COMBOBOX中了 现在想通过这个BUTTON确认按钮 实现将COMBOBOX选中表中的内容显示到datagirdview中,该怎么在button里面写代码啊?
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataTable a = new DataTable();
dataGridView1.DataSource = comboBox2.SelectedItem;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex > -1)
{
SqlConnection conn = new SqlConnection("data source=localhost;user id=sa;password=1");
conn.Open();
string str = "SELECT name FROM [" + comboBox1.SelectedItem.ToString() + "]..SysObjects WHERE XType='U' ORDER BY name";
SqlCommand cmd = new SqlCommand(str, conn);
SqlDataReader dr = cmd.ExecuteReader();
comboBox2.Items.Clear();
while (dr.Read())
{
comboBox2.Items.Add(dr[0]);
}
conn.Close();
}
} 展开
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataTable a = new DataTable();
dataGridView1.DataSource = comboBox2.SelectedItem;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex > -1)
{
SqlConnection conn = new SqlConnection("data source=localhost;user id=sa;password=1");
conn.Open();
string str = "SELECT name FROM [" + comboBox1.SelectedItem.ToString() + "]..SysObjects WHERE XType='U' ORDER BY name";
SqlCommand cmd = new SqlCommand(str, conn);
SqlDataReader dr = cmd.ExecuteReader();
comboBox2.Items.Clear();
while (dr.Read())
{
comboBox2.Items.Add(dr[0]);
}
conn.Close();
}
} 展开
1个回答
展开全部
拿到combobox选中的数据源 comboBox1.SelectedItem ;
你是用数据库表直接绑定的话,这个对象就是表里面的一行数据,也就是DataRow
直接设置到datagirdview的数据源中就行了
dataGridView1.DataSource = comboBox1.SelectedItem;
你是用数据库表直接绑定的话,这个对象就是表里面的一行数据,也就是DataRow
直接设置到datagirdview的数据源中就行了
dataGridView1.DataSource = comboBox1.SelectedItem;
追问
private void button_Click_1(object sender, EventArgs e)
{
}
我该怎么在这个button里面写呢
或是在其他事件里写啊
追答
private void button_Click_1(object sender, EventArgs e)
{
dataGridView1.DataSource = comboBox1.SelectedItem;
}
这样就可以了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询