Click here to Skip to main content
13,287,658 members (45,828 online)
Rate this:
Please Sign up or sign in to vote.
Hi All,

I have created a gridview at runtime.
I want to make the columns of the gridview sortable.

i am adding colums to the gridview columns dynamically by using the below code

gv.AllowSorting = true;
gv.RowCreated += new GridViewRowEventHandler(gv_RowCreated);
gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
gv.Sorting += new GridViewSortEventHandler(gv_Sorting);
if (gv.Columns.Count == 0)
                foreach (DataColumn col in dtUserDetails.Columns)
                    TemplateField bfield = new TemplateField();
                    bfield.HeaderTemplate = new GridViewTemplate.GridViewTextTemplate(ListItemType.Header, col.ColumnName, col.DataType.Name);
                    bfield.ItemTemplate = new GridViewTemplate.GridViewTextTemplate(ListItemType.Item, col.ColumnName, col.DataType.Name);
                    bfield.EditItemTemplate = new GridViewTemplate.GridViewTextTemplate(ListItemType.EditItem, col.ColumnName, col.DataType.Name);
                    bfield.SortExpression = col.ColumnName;

I have RowCreated, RowDataBound, and Sorting event handler for the gridview.
But the gridview column are not clickable for firing the sort event.
So what i need to do to make it sortable.

Thanks for your help in advance.
Posted 25-Jun-12 1:40am
Updated 25-Jun-12 2:06am
GanesanSenthilvel 25-Jun-12 7:06am
Code format

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

this.gv.Sort(this.gv.Columns["Sorted Column Name"], ListSortDirection.Descending(or) ascending);

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171207.1 | Last Updated 25 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100