qq_21598937
2022-03-25 11:11
采纳率: 100%
浏览 22

vs2017,C# asp gridview控件绑定动态查询语句后,查询结果点击列标题排序后数据消失,重新查询才能得到排序结果

问题相关代码,请勿粘贴截图

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
但是点击查询结果的列标题进行排序后,数据消失。再次点击查询可正常显示排序后的结果。

我的解答思路和尝试过的方法 无
怎么更改正常排序而无需再次点击查询Button3_Click

3条回答 默认 最新

相关推荐 更多相似问题