Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 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

C#
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;

                    gv.Columns.Add(bfield);

                }
            }


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
Updated 25-Jun-12 1:06am
v2
Comments
Ganesan Senthilvel 25-Jun-12 7:06am    
Code format

1 solution

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

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