请教一下c#点一个树节点怎把值传给dataGridView1
如果是传值给textBox,可以这样写,
string sql = string.Format("select price,color,products_name from products where tid=" + e.Node.Tag, conn);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader cd = cmd.ExecuteReader();
while (cd.Read())
{
this.textBox2.Text = cd[0].ToString();
this.textBox3.Text = cd[1].ToString();this.textBox4.Text = cd[2].ToString();
但是dataGridView1的怎写呢?
[code="C#"]
string sql = string.Format("select price,color,products_name from products where tid=" + e.Node.Tag, conn);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt=new DataTable();
dt.Load(dr);
dataGridView1.DataSource = dt;
dataGridView1.DataBind();
[/code]
你可以把查询结果放到一个DataTable中,然后把dataGridView1的DataSource=这个DataTable:
[code="C#"]
string sql = string.Format("select price,color,products_name from products where tid=" + e.Node.Tag, conn);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = com.ExecuteReader();
DataTable dt=new DataTable();
dt.Load(dr);
dataGridView1.DataSource = dt;
dataGridView1.DataBind();
[/code]