c#中gridview控件的操作

来源:百度知道 编辑:UC知道 时间:2024/06/25 09:20:13
我想根据DropDownList控件来选择一个书籍类型,例如“计算机类”,然后点击“确定”,最后由gridview来显示全部“计算机类”的书籍,其他的都不输出!!!最好能有具体的后台代码!!谢谢!!!

private DataTable getDataSource(string BookType)
{
string sql="select * from Book where BookType="+BookType;
SqlConnection con=new SqlConnection(ConnectionString);
SqldataAdapter da=new SqldataAdapter(sql,con);
DataTable dt=new DataTable();
da.Fill(dt);
return dt;
}
gridview.DataSource=getDataSource(DropDownList1.Text);
gridview.DataBind();

先把datalist的postback勾选。
在datalist的selectindexchanged中写代码

在DropDownList控件的更改选择项的事件(selectindexchanged)中编写SQL语句,用DropDownList控件中选择的书籍类型作为条件查询数据库,然后绑定DataGridVeiw的DataSource,搞定!

一楼回答的基本正确!