asp.net在gridview中显示数据 [英] asp.net display data in gridview
问题描述
大家好..我需要面对我的问题的指导
我想在网格视图中显示绑定到sql数据源的数据
,但是问题部分是我想将该数据源的字段绑定到下拉列表,当在该下拉列表中选择一个项目时,与之对应的数据将显示在gridview中...
我已经完成了数据源和gridview的绑定...但没有cropdownlist的绑定... plz帮助伙伴.我会非常感激……在此先感谢您:)
这是我在周日使用
hey all.. i need a guidence to my problem im facing
i want to display data in gridview binded to a sql datasource
but the problem part is i want to bind a field of that datasource to a dropdownlist and when a item is selected in that dropdownlist then correcposnding to that data will be displayed in gridview...
i have done the binding of datasource and gridview... but not of cropdownlist...plz help buddies... i''ll be very grateful ... thanks in advance :)
here is my code which im using
<br />protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)<br /> { ///int count=0;<br /> con = new SqlConnection("Data Source=.;Initial Catalog=abc;Integrated Security=True");<br /><br /><br /> <br /> <br /> con = new SqlConnection("Data Source=.;Initial Catalog=abc;Integrated Security=True");<br /> cmd = new SqlCommand("select * from salescounts where =''"+DropDownList1.Text+"''", con);<br /> con.Open();<br /> SqlDataReader dr = cmd.ExecuteReader();<br /> if (dr.HasRows)<br /> {<br /> GridView1.DataSource = dr;<br /> GridView1.DataBind();<br /> Label1.Text = "Result found:" + GridView1.Rows.Count;<br /><br /><br /> }<br /> else<br /> {<br /> Label1.Text = "not found";<br /> }<br /><br /> con.Close();<br /> <br />}<br />
推荐答案
只需尝试以下一个方法
Just Try this one
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)<br /> { <br /> DataSet Ds=new DataSet();<br /> con = new SqlConnection("Data Source=.;Initial Catalog=abc;Integrated Security=True");<br /> cmd = new SqlCommand("select * from salescounts where =''"+DropDownList1.Text+"''", con);<br /> SqlDataAdapter DA = new SqlDataAdapter(cmd);<br /> DA.Fill(ds); <br /> if(Ds.Tables[0].Rows.Count>0)<br /> {<br /> GridView1.DataSource = ds;<br /> GridView1.DataBind();<br /> Label1.Text = "Result found:" + GridView1.Rows.Count;<br /><br /> }<br /> else<br /> {<br /> Label1.Text = "not found";<br /> }<br /><br /> con.Close();<br /> <br />}
快速建议:
Quick Suggestion :
cmd = new SqlCommand(从salescounts中选择*,其中=' '"+ DropDownList1.Text +"'',con);
cmd = new SqlCommand("select * from salescounts where =''"+DropDownList1.Text+"''", con);
请不要从UI级别执行此类SQL查询.它将导致SQL注入.始终使用参数化查询/存储过程.
Thansk :-D
Never Execute SQL Query like this from UI level. It will causes SQL Injection. Always used Parameterized query / Stored Procedure .
Thansk :-D
这篇关于asp.net在gridview中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!