Want To Sort the Content of the Gridview while clicking any of the Columns headertext

Posted by .Netlearner under ASP.NET on 6/8/2012 | Points: 10 | Views : 1158 | Status : [Member] | Replies : 4
Hai All,

Am Having a gridview in which i display the product and its price and Category. Sample Data is as follows,

Category Product Price
---------------------------------------------------------------
Soap Dove Soap 50.00
Handbag CherryRed M1 325.00

when i click the header of any column in the grid entire data must get sorted according to the selected column in ascending order.Can it be Done with ASP.NET Gridview and How this can be done.Please help with the sample code ?




Responses

Posted by: Santhi on: 6/8/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Whether you want to display all the data in the ascending order only after you click the header of any column in the gridview.
or you just want to display all the data in the ascending order by default.

Thanks & Regards,
Santhi .V

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rabbil on: 6/8/2012 [Member] Starter | Points: 25

Up
0
Down
yeah, it can be done in the grid view, i hope u might be using templetefield (and if not then you should use it ) inside that take an ItemTemplate
i'm giving an example for only one column,you create it more as per your requirements.

<asp:TemplateField HeaderText = "Product Name" SortExpression="Name">
<ItemTemplate>
<asp:LinkButton ID=ProductNameLinkButton" runat="server" ItemStyle-HorizontalAlign="Left" EnableViewState="true" Text='<%#DataBinder.Eval(Container.DataItem,"ProductName")%>' // here 'ProductName' is the column name of your table
></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

the .cs file u can use LINQ for sorting . also u must create a session for this and pass the object to this session.
simple ex: for the same


List<YourEntityClass> listObjectForSorting = (List<YourEntityClass>)Session["GridDataForSorting"];
if (sortExp == "Name")
{
var reslst = from l in listObjectForSorting orderby l.ProductName ascending select l;

listObjectForSorting = reslst.ToList();
}

//finally at the end of all

adminGridViewId.DataSource = listObjectForSorting;
adminGridViewId.DataBind();
Session["GridDataForSorting"] = listObjectForSorting;


Hope this would help you...
let me know for any clarifications...
happy coding,
Regards,
Rabbil Ahmad

"I cannot teach anybody anything. I can only make them think"~ Socrates
Thanks And Regards

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: .Netlearner on: 6/8/2012 [Member] Starter | Points: 25

Up
0
Down
Am using Bound Field Can it be done in the similar way

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rabbil on: 6/8/2012 [Member] Starter | Points: 25

Up
0
Down

No, this is not possible in BoundField,because in bound feild we can only bind the data directly but we can't customise it,
so according to the given case above, we can have a link button on the header column (for which we must use custom tag )i.e we can only do in TemplateField for the custom display of content.

any more clarifications would be highly accepted...!!



"I cannot teach anybody anything. I can only make them think"~ Socrates
Thanks And Regards

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response