如何在asp.net中创建排序? [英] how create sorting in asp.net?

查看:100
本文介绍了如何在asp.net中创建排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题.如何在asp.net中创建排序
我设置了gridview比例自动排序,但没有得到,发生了错误.
请给我解决方法.


HTML是:

i have a small question. how do I create sorting in asp.net
i am set the gridview proporties autosorting but not get is error occured.
please give me solution.


HTML is:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True"

            onsorting="GridView1_Sorting" PageSize="4">
        </asp:GridView>




编码为:




coding is :

public partial class _7sorting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         if(Page.IsPostBack==false)
            getdata();
      }
 
      private void getdata()
        {
            SqlConnection con = new SqlConnection("server=.;user id=prabha; password=prabha; database=asp");
            SqlDataAdapter da = new SqlDataAdapter("select * from empdetails", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "empdetails");
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }

推荐答案

好朋友,
当您在后面的代码中填充数据时,不允许排序

这里该怎么做

hy freind,
when you fill your data in code behind,allow sorting will not work

here how to do this

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
 {
     GridView1.DataSource = GetData(); // GetData() is a method that will get Data from Database/Cache/Session to display in Grid.
     GridView1.DataBind();
     GridView1.PageIndex = e.NewPageIndex;
 }


< asp:GridView
ID ="gvCountry" runat =服务器"
AutoGenerateColumns ="False" Width ="100%" AllowPaging ="True"
ShowFooter ="True" AllowSorting ="True"
onpageindexchanging ="gvCountry_PageIndexChanging" onsorting ="gvCountry_Sorting"><列>
< asp:TemplateField>< ItemTemplate>
< asp:ImageButton ID ="btnEdit" runat ="server"
CommandArgument =''<%#Eval("CountryId")%>''ImageUrl =〜/Images/edit.png"
CausesValidation ="False" onclick ="btnEdit_Click"/></ItemTemplate>
< FooterTemplate>
< asp:ImageButton ID ="btnSaveCountry" runat ="server"
onclick ="btnSaveCountry_Click"/>
</FooterTemplate>
</asp:TemplateField>
< asp:TemplateField HeaderText ="CountryId" SortExpression ="CountryId">
< ItemTemplate>
< asp:标签ID ="lblCountryId" runat =服务器" Text =''<%#Eval("CountryId")%>''></asp:Label>
</ItemTemplate>
< FooterTemplate>
< asp:TextBox ID ="txtCountryId" runat ="server" Enabled ="False"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
< asp:TemplateField HeaderText ="ECountryName" SortExpression ="ECountryName">
< ItemTemplate>
< asp:标签ID ="lblECountryName" runat =服务器"
Text =''<%#Eval("ECountryName")%>''></asp:Label>
</ItemTemplate>
< FooterTemplate>
< asp:TextBox ID ="txtECountryName" runat ="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
< asp:TemplateField HeaderText ="LCountryName" SortExpression ="LCountryName">
< ItemTemplate>
< asp:标签ID ="lblLCountryName" runat =服务器"
Text =''<%#Eval("LCountryName")%>''></asp:Label>
</ItemTemplate>
< FooterTemplate>
< asp:TextBox ID ="txtLCountryName" runat ="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
< asp:TemplateField HeaderText ="CountryCode" SortExpression ="CountryCode">
< ItemTemplate>
< asp:标签ID ="lblCountryCode" runat =服务器" Text =''<%#Eval("CountryCode")%>''></asp:Label>
</ItemTemplate>
< FooterTemplate>
< asp:TextBox ID ="txtCountryCode" runat ="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>< AlternatingRowStyle CssClass ="gridAltRow"></AlternatingRowStyle>< HeaderStyle CssClass ="gridheader"/>< PagerStyle
CssClass ="gridheader"/>< RowStyle CssClass ="gridrow" Horizo​​ntalAlign ="Center"></RowStyle></asp:GridView>
后面的代码为网格生成排序事件
受保护的void gvCountry_Sorting(对象发送者,GridViewSortEventArgs e)
{
DataTable dt =(DataTable)Session ["dtFilter"];
字符串sortdir ="DESC";
如果(ViewState ["SortDir"]!= null)
{
sortdir = ViewState ["SortDir"].ToString();
如果(sortdir =="ASC")
{
e.SortDirection = SortDirection.Descending;
ViewState ["SortDir"] ="DESC";
}
其他
{
e.SortDirection = SortDirection.Ascending;
ViewState ["SortDir"] ="ASC";
}
}
其他
{
ViewState ["SortDir"] ="DESC";
}
dt =(新的DataView(dt,",e.SortExpression +" + ViewState ["SortDir"].ToString(),DataViewRowState.CurrentRows)).ToTable();
gvCountry.DataSource = dt;
gvCountry.DataBind();
}
<asp:GridView
ID="gvCountry" runat="server"
AutoGenerateColumns="False" Width="100%" AllowPaging="True"
ShowFooter="True" AllowSorting="True"
onpageindexchanging="gvCountry_PageIndexChanging" onsorting="gvCountry_Sorting"><Columns>
<asp:TemplateField><ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server"
CommandArgument=''<%# Eval("CountryId") %>'' ImageUrl="~/Images/edit.png"
CausesValidation="False" onclick="btnEdit_Click" /></ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="btnSaveCountry" runat="server"
onclick="btnSaveCountry_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CountryId" SortExpression="CountryId">
<ItemTemplate>
<asp:Label ID="lblCountryId" runat="server" Text=''<%# Eval("CountryId") %>''></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCountryId" runat="server" Enabled="False"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ECountryName" SortExpression="ECountryName">
<ItemTemplate>
<asp:Label ID="lblECountryName" runat="server"
Text=''<%# Eval("ECountryName") %>''></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtECountryName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LCountryName" SortExpression="LCountryName">
<ItemTemplate>
<asp:Label ID="lblLCountryName" runat="server"
Text=''<%# Eval("LCountryName") %>''></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLCountryName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CountryCode" SortExpression="CountryCode">
<ItemTemplate>
<asp:Label ID="lblCountryCode" runat="server" Text=''<%# Eval("CountryCode") %>''></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCountryCode" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns><AlternatingRowStyle CssClass="gridAltRow" ></AlternatingRowStyle><HeaderStyle CssClass="gridheader" /><PagerStyle
CssClass="gridheader" /><RowStyle CssClass="gridrow" HorizontalAlign="Center" ></RowStyle></asp:GridView>
Code Behind Generate Sorting Event for Grid
protected void gvCountry_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = (DataTable)Session["dtFilter"];
string sortdir = "DESC";
if (ViewState["SortDir"] != null)
{
sortdir = ViewState["SortDir"].ToString();
if (sortdir == "ASC")
{
e.SortDirection = SortDirection.Descending;
ViewState["SortDir"] = "DESC";
}
else
{
e.SortDirection = SortDirection.Ascending;
ViewState["SortDir"] = "ASC";
}
}
else
{
ViewState["SortDir"] = "DESC";
}
dt = (new DataView(dt, "", e.SortExpression + " " + ViewState["SortDir"].ToString(), DataViewRowState.CurrentRows)).ToTable();
gvCountry.DataSource = dt;
gvCountry.DataBind();
}


此链接将为您提供很多帮助

http://highoncoding.com/Articles/176_Sorting_GridView_Manually_.aspx [
This Link will help you a lot

http://highoncoding.com/Articles/176_Sorting_GridView_Manually_.aspx[^]

Add this code to your code

//declare global variables
private const string ASCENDING = " ASC";
        private const string DESCENDING = " DESC";


public SortDirection GridViewSortDirection

{

get

{

if (ViewState["sortDirection"] == null)

ViewState["sortDirection"] = SortDirection.Ascending;

return (SortDirection) ViewState["sortDirection"];

}

set { ViewState["sortDirection"] = value; }

}


GridViewSortDirection是一个简单的属性,它为GridView控件返回新的排序方向.因为,列的标题触发了回发,这就是为什么我将最后一个排序方向保存到ViewState对象中的原因.一次,我知道了我可以给用户新的排序方向的最后一个方向.这意味着,如果列按升序排序,则新方向必须是降序.

现在,让我们看一下单击列标题对其进行排序时触发的GridView_OnSorting事件.


The GridViewSortDirection is a simple property which returns the new sort direction for the GridView control. Since, the header of the column triggers a postback that is why I am saving the last sort direction into the ViewState object. Once, I know the last direction I can give the user the new sort direction. This means that if the column was sorted in ascending order then the new direction has to be descending.

Now, let''s take a look at the GridView_OnSorting event which is fired when you click the header of the column to sort it.

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)

{

string sortExpression = e.SortExpression;

if (GridViewSortDirection == SortDirection.Ascending)

{

GridViewSortDirection = SortDirection.Descending;

SortGridView(sortExpression, DESCENDING);

}

else

{

GridViewSortDirection = SortDirection.Ascending;

SortGridView(sortExpression, ASCENDING);

}

}


第一行获取被单击的列的名称.这意味着,如果您单击CategoryName列,则e.SortExpression将包含"CategoryName".代码很简单,我检查最后一个排序方向是否是升序,然后按降序对数据进行排序,反之亦然. SortGridView方法负责实际的排序.看看下面给出的SortGridView方法:


The first line gets the name of the column that is clicked. This means that if you clicked on the CategoryName column the e.SortExpression will contain "CategoryName". The code is pretty simple, I check that if the last sort direction is ascending if so, then I sort the data in descending order and vice versa. The SortGridView method is responsible for the actual sort. Take a look at the SortGridView method given below:

private void SortGridView(string sortExpression,string direction)

{

// You can cache the DataTable for improving performance

DataTable dt = GetData().Tables[0];

DataView dv = new DataView(dt);

dv.Sort = sortExpression + direction;

GridView1.DataSource = dv;

GridView1.DataBind();

}


这篇关于如何在asp.net中创建排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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