Click here to Skip to main content
15,897,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
XML
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
            onsorting="GridView1_Sorting" PageSize="4">
        </asp:GridView>



coding is :
C#
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();
        }
Posted
Updated 15-Sep-11 5:09am
v2

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

here how to do this

C#
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;
 }
 
Share this answer
 
<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();
}
 
Share this answer
 
Comments
rkthiyagarajan 17-Sep-11 6:54am    
Dear one don't forgot to use pre tag.
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
C#
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; }

}

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);

}

}

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();

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900