1、控件代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource6" AllowSorting="True" DataKeyNames="id" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" >
<asp:ButtonField CommandName="select" DatatextField="xm" HeaderText="姓名" SortExpression="xm" /> <%--将姓名列作为行选中列--%>
<asp:TemplateField HeaderText="出生日期" >
<ItemTemplate>
<%# Eval("sfzhm").ToString().Substring(9,6) %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="phone" HeaderText="联系电话" SortExpression="phone" />
<asp:TemplateField HeaderText="来源地信息" SortExpression="mcsheng,mcshi,mcxian">
<ItemTemplate>
<%#Eval("mcsheng") %> <%#Eval("mcshi") %> <%#Eval("mcxian") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="归属地信息" SortExpression="mczhen,mcscj">
<ItemTemplate>
<%#Eval("mczhen") %> <%#Eval("mcscj") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="xxdz" HeaderText="居住地详址" SortExpression="xxdz" />
<asp:BoundField DataField="rqdh" HeaderText="来海日期" SortExpression="rqdh" />
<asp:BoundField DataField="sbdw" HeaderText="上报单位" SortExpression="sbdw" />
<asp:TemplateField HeaderText="上报日期" SortExpression="sbsj" >
<ItemTemplate>
<%#Convert.ToDateTime(Eval("sbsj")).ToString("yy-MM-dd")%> <%--将datetime时间转换成yy-mm-dd时间格式--%>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowDeleteButton="false" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:xuexiConnectionString %>" SelectCommand='<%# GetSelectSql() %>'>
</asp:SqlDataSource>
2、 c#代码
public partial class Xcx1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string GetSelectSql() ////判断选择条件生成查询sql语句
{
StringBuilder sql = new StringBuilder("select * from gaxx ");
List<string> wheres = new List<string>();
//判断选择了那些条件并生成相应的sql字段
if (DropDownList1.SelectedIndex != 0)
{ wheres.Add(" mcsheng="+ "'"+ DropDownList1.SelectedItem.Text+"'" );
}
if (DropDownList2.SelectedIndex != 0)
{
wheres.Add(" mcshi=" + "'" + DropDownList2.SelectedItem.Text + "'");
}
if (DropDownList3.SelectedIndex != 0)
{
wheres.Add(" mcxian=" + "'" + DropDownList3.SelectedItem.Text + "'");
}
if (DropDownList4.SelectedIndex != 0)
{
wheres.Add(" mczhen=" + "'" + DropDownList4.SelectedItem.Text + "'");
}
if (DropDownList5.SelectedIndex != 0)
{
wheres.Add(" mcscj=" + "'" + DropDownList5.SelectedItem.Text + "'");
}
if (TextBox1.Text != "")
{
wheres.Add("xm=" + "'" + TextBox1.Text + "'");
}
if (TextBox2.Text != "")
{
wheres.Add("sfzhm=" + "'" + TextBox2.Text + "'");
}
if (TextBox3.Text != "")
{
wheres.Add("phone=" + "'" + TextBox3.Text + "'");
}
if (TextBox4.Text != "")
{
wheres.Add("rqdh>" + "'" + TextBox4.Text + "'");
}
if (TextBox5.Text != "")
{
wheres.Add("rqdh<" + "'" + TextBox5.Text + "'");
}
if (wheres.Count > 0)
{
string wh = string.Join(" and ", wheres.ToArray());//用and 连接数组中的每一个值
sql.Append(" where " + wh);
}
return sql.ToString();
}
protected void Button3_Click(object sender, EventArgs e)//查询数据
{
//生成查询sql语句
GetSelectSql();
Label1.DataBind();//用于测试生成的sql语句正确性
//重新绑定数据源
SqlDataSource6.DataBind();
//重新绑定GridView控件
//GridView1.DataBind();
}
}
点击查询可正常查询
https://img-mid.csdnimg.cn/release/static/image/mid/ask/251360771846128.png
但是点击查询结果的列标题进行排序后,数据消失。再次点击查询可正常显示排序后的结果。
添加OnSorting事件,事件重新获取sql后绑定数据源
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//e.SortDirection;//方向
//e.SortExpression//点击的排序字段名
//题主可以根据e参数内容扩展下GetSelectSql方法,增加排序工功能
SqlDataSource6.SelectCommand = GetSelectSql();
SqlDataSource6.DataBind()
}
<asp:GridView OnSorting="GridView1_Sorting"
【温馨提示:若能帮到您,望给个采纳该答案哦,谢谢!】
1、原因
没有在load方法里进行
2、解决方法
在if (!IsPostBack)里调用绑定数据
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//SortExpression="sbsj"
//可以设置默认排序字段-上报时间
ViewState["SortOrder"] = "sbsj"; //字段名
ViewState["OrderDire"] = "DESC"; //DESC=降序,ASC升序
//生成查询sql语句
GetSelectSql();
Label1.DataBind();//用于测试生成的sql语句正确性
//重新绑定数据源
SqlDataSource6.DataBind();
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sPage = e.SortExpression;
if (ViewState["SortOrder"].ToString() == sPage)
{
if (ViewState["OrderDire"].ToString() == "Desc")
ViewState["OrderDire"] = "ASC";
else
ViewState["OrderDire"] = "Desc";
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
//生成查询sql语句
GetSelectSql();
Label1.DataBind();//用于测试生成的sql语句正确性
//重新绑定数据源
SqlDataSource6.DataBind();
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!