ASP.NET Gridview排序不起作用

时间:2017-10-30 13:13:15

标签: asp.net sorting gridview

我正在尝试使用asp.net在Web应用程序上实现gridview。在通过方法对数据网格进行排序时遇到问题 - 我想请教一下。这是我的.aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="GridViewDemo1.Employee" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:GridView 
        ID="grvEmployee" 
        runat="server" 
        AutoGenerateColumns="true"
        BackColor="AliceBlue"
        ForeColor="Goldenrod" 
        BorderColor="YellowGreen"
        BorderStyle="Groove"
        Width="70%"
        CellPadding="3"
        CellSpacing="2" 
        AllowSorting="True"
        OnSorting="GridView1_Sorting" 
        AutoGenerateEditButton="true" 
        AutoGenerateDeleteButton="true" ViewStateMode="Enabled">  


        <RowStyle 
            HorizontalAlign="Center">
        </RowStyle>

        <FooterStyle
            ForeColor="#8C4510"
            BackColor="#F7DFB5">
        </FooterStyle>

        <PagerStyle 
            ForeColor="#8C4510" 
            HorizontalAlign="Center">
        </PagerStyle>

        <HeaderStyle 
            ForeColor="White" 
            Font-Bold="True" 
            BackColor="#A55129">
        </HeaderStyle>

    </asp:GridView>
</div>
</form>

这是.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace GridViewDemo1
{
public partial class Employee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        string selectSQL = "SELECT * from dbo.[User]";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(selectSQL, con);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adapter.Fill(ds, "Employee");

        grvEmployee.DataSource = ds;
        grvEmployee.DataBind();

    }


    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        //dataTable.DefaultView.Sort = e.SortExpression;
        //grvEmployee.DataSource = dataTable;
        grvEmployee.DataBind();
    }


}
}

这是我的web.config连接字符串:

  <connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=xxxxxx;Initial Catalog=yyyyyyy;User ID=zzzzz;Password=xxxxxx;" providerName="System.Data.SqlClient" />

gridview被正确填充,我习惯于获得未被处理的&#34;触发事件排序。&#34;但现在我只是因为尝试对列进行排序而得不到任何回应。这甚至适用于自动生成的列吗?我在哪里可以指定排序表达式? (升序/降序等)?

1 个答案:

答案 0 :(得分:1)

只有!IsPostback不是每次连续回发时都必须执行初始DataBinding:

if(!IsPostBack)
{
    string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    string selectSQL = "SELECT * from dbo.[User]";
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(selectSQL, con);
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    adapter.Fill(ds, "Employee");

    grvEmployee.DataSource = ds;
    grvEmployee.DataBind();
}

GridView1_Sorting中,您必须从数据库中选择有序数据并将其分配给网格的DataSource属性,然后调用grvEmployee.DataBind()

GridView排序示例:https://stackoverflow.com/a/6602125/284240