我已经更改了SqlDataSource对象的select查询并将其绑定到GridView,就像这样
protected void Button1_Click(object sender, EventArgs e)
{
if (ddlBloodGroup.SelectedValue != "Any")
{
bg = " And BloodGroup='" + ddlBloodGroup.SelectedValue + "'";
}
if (ddlRh.SelectedValue != "Any")
{
rh = " And Rh='" + ddlRh.SelectedValue + "'";
}
q = "select * from Donor Where 1=1 "+bg+rh;
SqlDataSource1.SelectCommand = q;
SqlDataSource1.DataBind();
GridView1.DataBind();
}
我清楚地得到了结果
但是当我点击列标题按该列排序时,数据就消失了
答案 0 :(得分:0)
您必须处理排序事件
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (ddlBloodGroup.SelectedValue != "Any")
{
bg = " And BloodGroup='" + ddlBloodGroup.SelectedValue + "'";
}
if (ddlRh.SelectedValue != "Any")
{
rh = " And Rh='" + ddlRh.SelectedValue + "'";
}
q = "select * from Donor Where 1=1 "+bg+rh;
SqlDataSource1.SelectCommand = q;
SqlDataSource1.DataBind();
GridView1.DataBind();
}
当然你需要允许排序
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1" AllowSorting="True"
onsorting="GridView1_Sorting">