如何通过单击列标题对gridview中的数据进行排序 [英] How to sort data in gridview by clicking column header

查看:99
本文介绍了如何通过单击列标题对gridview中的数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET 3.5中有一个gridview,并且启用了Auto-Generated Filed,我希望通过单击网格标题对数据进行排序。我怎样才能做到这一点?我搜索了互联网,但大多数都使用html中的列,但是我没有,因为它们是自动生成的。谢谢

解决方案

为了排序,你必须绑定网格...



 私有  void  LoadGrid( string  sortExpr, string  sortDirection)
{


DataSet ds = new DataSet();
SqlDataAdapter SqlDA = new SqlDataAdapter( 选择*来自tablename order by + sortExpr + + sortDirection + ,connectionString);
SqlDA.Fill(ds);
if (ds!= null
{
gdManageForm。 DataSource = ds;
gdManageForm.EditIndex = -1;
gdManageForm.DataBind();
}

}

受保护 void gdManageForm_Sorting( object sender,GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
ViewState [ SortExpression] = sortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression,DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression,ASCENDING);
}
}

私有 void SortGridView( string sortExpression, string 方向)
{
// 如果是默认状态
LoadGrid(sortExpression,direction);
}

私人 SortDirection GridViewSortDirection
{
get
{
if (ViewState [ sortDirection] == null
ViewState [ sortDirection] = SortDirection.Ascending;
return (SortDirection)ViewState [ sortDirection ];
}
set {ViewState [ sortDirection] = value ; }
}

如果您有任何疑问请告诉我


 static SortDirection GridViewSortDirection; 
protected void chequegrid_Sorting(object sender,GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
ViewState [SortExpression] = sortExpression;
if(GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression,DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression,ASCENDING);
}
}

public void SortGridView(string sortExpression,string direction)
{
if(sortExpression ==ss)
{
string s;
if(direction ==DESCENDING)
{
s =order by dbo.registration.id desc;
}
else
{
s =dbo.registration.id的订单;
}

bindbysort(s);
}
}

public void bindbysort(string expre)
{
DataSet ds = new DataSet();
ds = ck.bindgrid(expre);
if(ds.Tables [0] .Rows.Count> 0)
{
chequegrid.DataSource = ds;
Label1.Text = ds.Tables [0] .Rows.Count.ToString()+Result Found;
chequegrid.DataBind();
}
其他
{
chequegrid.DataSource = null;
chequegrid.DataBind();

}
}





aspx





< asp:gridview id =   chequegrid runat =   server autogeneratecolumns =   False allowsorting =   true xmlns:asp =  #unknown >  
CellPadding = 4 ForeColor = #333333 GridLines = EmptyDataText = 没有生成任何检查但这段时间。
CssClass = table-bordered table g宽度= 100% onsorting = chequegrid_Sorting >
< alternatingrowstyle backcolor = 白色 forecolor = #284775 />
< columns>
< asp:templatefield headertext = 客户ID sortexpression = ss >

<&的ItemTemplate GT;
< asp:label id = Label1 runat = server >
Text = ' <%#Eval(customer_reg_id)%>' > < / asp:label >
< / itemtemplate >
< / asp:templatefield >
< asp:templatefield headertext = 客户名称 >

< itemtemplate>
< asp:label id = Label1 runat = server >
Text = ' <%#Eval(name)%>' > < / asp:label >
< / itemtemplate >
< / asp:templatefield >
< asp:templatefield headertext = Date From >

<&的ItemTemplate GT;
< asp:label id = Label1 runat = server text = <%#Convert.ToDateTime(Eval( date_from ))。ToString( dd MMMM,yyyy )%> > < / asp:label >
< / itemtemplate >
< / asp:templatefield >
< asp:templatefield headertext = Date To >

< ; ItemTemplate中>
< asp:label id = Label2 runat = server text = <%#Convert.ToDateTime(Eval( date_to ))。ToString( dd MMMM,yyyy )%> > < / asp:label >
< / itemtemplate >
< / asp:templatefield >
< asp:templatefield headertext = 检查金额 >

< ; ItemTemplate中>
< asp:label id = Label3 runat = server text = <%#Eval( income )+ / - %> > < / asp:label >
< / itemtemplate >
< / asp:templatefield >
< asp:templatefield headertext = Ge nerated On >

< itemtemplate>
< asp:label id = Label4 runat = server text = <%#Convert.ToDateTime(Eval( date_of_clear ))。ToString( dd MMMM,yyyy )%> > < / asp:label >
< / itemtemplate >
< / asp:templatefield >




< span class =code-keyword>< / >


< / asp:gridview >


I have a gridview in ASP.NET 3.5 and Auto-Generated Filed is enabled and I want to sort data by clicking the grid header. how can I do that? I searched internet but most of it work with the columns from html but mine don't have since they are auto generated. Thanks

解决方案

For sorting you have to bind the grid...

private void LoadGrid(string sortExpr, string sortDirection)
{


        DataSet ds = new DataSet();
        SqlDataAdapter SqlDA = new SqlDataAdapter("Select * from tablename order by "+sortExpr+"  "+sortDirection+"", connectionString);
        SqlDA.Fill(ds);
        if (ds != null)
        {
            gdManageForm.DataSource = ds;
    gdManageForm.EditIndex = -1;
    gdManageForm.DataBind();
        }

}

protected void gdManageForm_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortExpression = e.SortExpression;
    ViewState["SortExpression"] = sortExpression;
    if (GridViewSortDirection == SortDirection.Ascending)
    {
        GridViewSortDirection = SortDirection.Descending;
        SortGridView(sortExpression, DESCENDING);
    }
    else
    {
        GridViewSortDirection = SortDirection.Ascending;
        SortGridView(sortExpression, ASCENDING);
    }
}

private void SortGridView(string sortExpression, string direction)
{
    //If it is default state
    LoadGrid(sortExpression, direction);
}

private SortDirection GridViewSortDirection
{
    get
    {
        if (ViewState["sortDirection"] == null)
            ViewState["sortDirection"] = SortDirection.Ascending;
        return (SortDirection)ViewState["sortDirection"];
    }
    set { ViewState["sortDirection"] = value; }
}

If you have any doubts let me know


static SortDirection GridViewSortDirection;
 protected void chequegrid_Sorting(object sender, GridViewSortEventArgs e)
   {
       string sortExpression = e.SortExpression;
       ViewState["SortExpression"] = sortExpression;
       if (GridViewSortDirection == SortDirection.Ascending)
       {
           GridViewSortDirection = SortDirection.Descending;
           SortGridView(sortExpression, "DESCENDING");
       }
       else
       {
           GridViewSortDirection = SortDirection.Ascending;
           SortGridView(sortExpression, "ASCENDING");
       }
   }

   public void SortGridView(string sortExpression, string direction)
   {
       if (sortExpression == "ss")
       {
           string s;
           if(direction =="DESCENDING")
           {
               s = "order by dbo.registration.id desc";
           }
           else
           {
               s = "order by dbo.registration.id";
           }

           bindbysort(s);
       }
   }

   public void bindbysort(string expre)
   {
       DataSet ds = new DataSet();
       ds = ck.bindgrid(expre);
       if (ds.Tables[0].Rows.Count > 0)
       {
           chequegrid.DataSource = ds;
           Label1.Text = ds.Tables[0].Rows.Count.ToString() + " Result Found";
           chequegrid.DataBind();
       }
       else
       {
           chequegrid.DataSource = null;
           chequegrid.DataBind();

       }
   }



aspx


<asp:gridview id="chequegrid" runat="server" autogeneratecolumns="False" allowsorting="true" xmlns:asp="#unknown">
    CellPadding="4" ForeColor="#333333" GridLines="None" EmptyDataText="There is no any cheque generated yet for this time duration."
     CssClass="table-bordered table g" Width="100%" onsorting="chequegrid_Sorting">
    <alternatingrowstyle backcolor="White" forecolor="#284775" />
    <columns>
       <asp:templatefield headertext="Customer Id" sortexpression="ss">
            
            <itemtemplate>
                <asp:label id="Label1" runat="server">
                    Text='<%#  Eval("customer_reg_id") %>'></asp:label>
            </itemtemplate>
        </asp:templatefield>
         <asp:templatefield headertext="Customer Name">
            
            <itemtemplate>
                <asp:label id="Label1" runat="server">
                    Text='<%#  Eval("name") %>'></asp:label>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Date From">
            
            <itemtemplate>
                <asp:label id="Label1" runat="server" text="<%#  Convert.ToDateTime(Eval("date_from")).ToString("dd MMMM, yyyy") %>"></asp:label>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Date To">
            
            <itemtemplate>
                <asp:label id="Label2" runat="server" text="<%# Convert.ToDateTime(Eval("date_to")).ToString("dd MMMM, yyyy") %>"> </asp:label>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Cheque Amount">
            
            <itemtemplate>
                <asp:label id="Label3" runat="server" text="<%# Eval("income") +"/-" %>"></asp:label>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Generated On">
            
            <itemtemplate>
                <asp:label id="Label4" runat="server" text="<%# Convert.ToDateTime(Eval("date_of_clear")).ToString("dd MMMM, yyyy") %>"></asp:label>
            </itemtemplate>
        </asp:templatefield>
       
       
      
       
    </columns>
 

</asp:gridview>


这篇关于如何通过单击列标题对gridview中的数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆