c# 关于combobox控件和datagrid 的问题

来源:百度知道 编辑:UC知道 时间:2024/05/29 06:05:34
如何根据combobox选定的值,来读取数据库表中的值,并把它显示在datagrid中

private void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string combovalue = comboBox1.SelectedValue.ToString();
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = "连接字符串";
SqlCommand cmd = new SqlCommand();

cmd.CommandText = "sql语句";
//sql语句 例: "select * from demo where fruit = '+combovalue+'"
//或者用 String.Format("select * from demo where fruit = '{0}'",combovalue);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
}
}

太不详细了 发邮件我 里面详细说 我邮箱snake1964@126.com

第一个把你查询出来的值作为你Select查询的条件就行了!第二个你可以把查询的数据直接