how to enable sorting in gridview after change header text

sorting gridview in ascending and descending order in asp net
asp.net gridview sorting
gridview allow sorting
gridview sorting not working
the gridview 'gridview1' fired event sorting which wasn't handled.
vb net gridview sorting
how to sort data in gridview c#
gridview sorting with arrows in asp net

I have a gridview in my web page to show my query result

I wrote necessary function to sort by clicking on column's title

but when i change title in code behind i can't sort by clicking

how can i change column format from text to hyperlink after binded data ?


I already faced this "error" too. Trick is to avoid converting Header to string, when you changing it's Text value (you are probably using e.Row.Cells[0].Text = "This is changed header 1" in your code behind). After this all changed columns will be with disabled sorting. So, you need to treat header not as text, but as LinkButton and everything will be perfect.

This is my solution :

I am using OnRowDataBound="Exmplgrid_RowDataBound" in my grid definition. If you will use this, sorting will stay enabled on first 3 columns and disabled in 4th (for demo purposes):

protected void Exmplgrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            LinkButton btnSort = (LinkButton)e.Row.Cells[0].Controls[0];
            btnSort.Text = "This is changed header 1";
            btnSort = (LinkButton)e.Row.Cells[1].Controls[0];
            btnSort.Text = "This is changed header 2";
            btnSort = (LinkButton)e.Row.Cells[2].Controls[0];
            btnSort.Text = "This is changed header 2";
            e.Row.Cells[3].Text = "This is changed header 2. SORTING DISABLED"
        }

    }

c#, I have a gridview in my web page to show my query result. I wrote necessary function to sort by clicking on column's title. but when i change title in code behind i  i have a gridview in my web page to show my query result . i wrote necessary function to sort by clicking on column's title . but when i change title in code behind i can't sort by clicking . how can i change column format from text to hyperlink after binded data ?


In order to provide a better answer please release some codes for fix!

Working with grid views in web pages are a bit tough but some experts release out controls which you can use for developing a web site or a web app you can have look at following link and even evaluate other controls like WYSIWYG Editor, scheduler,tree view,Menus and more on Telerik Asp.net controls.

Grid - Programmatic Sorting

RadControls for ASP.NET AJAX

enable sorting in gridview after change header text, i have a gridview in my web page to show my query result i wrote necessary function to sort by clicking on column's title but when i change title  i can set my gridview to sort . but my problem is when i want to change header text in code behind the header text is not like hyperlink then i can't click on new text to call sort function try this :


Use THis code for Sorting........

 protected void RadgvData_SortCommand(object sender, GridSortCommandEventArgs e)
        {
            GridTableView tableView = e.Item.OwnerTableView;
            e.Canceled = true;
            GridSortExpression expression = new GridSortExpression();
            expression.FieldName = e.SortExpression;
            if (tableView.SortExpressions.Count == 0 || tableView.SortExpressions[0].FieldName != e.SortExpression)
            {
                expression.SortOrder = GridSortOrder.Descending;
            }
            else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Descending)
            {
                expression.SortOrder = GridSortOrder.Ascending;
            }
            else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Ascending)
            {
                expression.SortOrder = GridSortOrder.Descending;
            }

            tableView.SortExpressions.AddSortExpression(expression);
            RadgvData.Rebind();

        }

GridView.AllowSorting Property (System.Web.UI.WebControls , Gets or sets a value indicating whether the sorting feature is enabled. Edit. Share. Twitter · LinkedIn · Facebook · Email. Table of contents The following example demonstrates how to use the AllowSorting property to enable sorting in a <asp:boundfield datafield="City" headertext="City" headerstyle-wrap="false"​  To enable sorting, set the AllowSorting property to true. When sorting is enabled, the heading text for each column field with its SortExpression property set is displayed as a link button.


How to: Sort a GridView Column When a Header Is Clicked, How to: Sort a GridView Column When a Header Is Clicked The following example defines a GridView with three columns that bind to the the sort direction changes between ascending order and descending order each  If this is the case, then to sort manually, one method is to add an OnSorting callback to the GridView, SortExpression's to your fields and a method to callback in your code-behind. This would result in markup and code similar to (untested):


Beginning ASP.NET 2.0 Databases: Beta Preview, <asp:GridView ID=”GridView1” Runat=”server” AutoGenerateColumns=true Enable. Sorting. As you saw in the preceding simple case, sorting works with a single columns, you will have to ensure that you meet the following requirements. HeaderText—This property is set on a field object (for example, BoundField) in  If you want to apply sorting for all of your columns, here is a more generic solution of mine; Make sure you subscribe your data grid to the event ColumnHeaderMouseClick. When the user clicks on the column it will sort by descending. If the same column header is clicked again, sorting will be applied by ascending.


Changing RadGrid Column Header Text when Sorting is Enabled , Changing RadGrid Column Header Text when Sorting is Enabled. https://docs.​telerik.com/devtools/aspnet-ajax/controls/grid/how-to/Common/using-the-- You can use one of the following approaches to resolve the issue: To sort the data according to the contents of a column, the example defines an event handler to handle the Click event that occurs when you press the column header button. The following example shows how to specify an event handler for the GridViewColumnHeader control. <ListView x:Name='lv' Height="150" HorizontalAlignment="Center