qq_21598937 2022-03-25 11:11 采纳率: 100%
浏览 53
已结题

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条回答 默认 最新

  • CSDN专家-showbo 2022-03-25 11:48
    关注

    添加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" 
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月2日
  • 已采纳回答 3月25日
  • 创建了问题 3月25日

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料