.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Santhakumar Munuswamy
Asad Ali
Fauzul Azmi
Post New Web Links

How to sort GridView?

Posted By: Venkat     Posted Date: April 07, 2010    Points: 2   Category :ASP.Net
In this article I will discuss how to sort columns in grid and more. At the end it will help answer some very frequently asked questions like following

View Complete Post

More Related Resource Links

Sort by gridview SortExpression parameter via Stored Procedure


I have a gridview that calls data via a stored procedure.  I am unable to enable the gridview columns to be sortable. I need to set the parameter in the Stored Procedure, can someone help me with this?

Here is my gridview:

<asp:GridView ID="AllUsersGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="UserName"
                        GridLines="Vertical" Width="900px" DataSourceID="SqlDataSource1" AllowSorting="True"
                        SelectedRowStyle-Height="30px" CellPadding="4" BackColor="White">
                            <asp:TemplateField HeaderText="Full Name" SortExpression="lastname">
                                    <asp:Label ID="DisplayName" runat="server" Text='<%# Eval("firstname").ToString() & " " & Eval("lastname").ToString() %>' />
                            <asp:BoundField HeaderText="User Name" DataField="UserName" />

Custom Headers in Gridview, need to be able to sort

I have a Custom Header that I need to beable to sort.  The headers consist of a Textbox (for entering a search value) and a Linkbutton to fire the event.  I just need to know how to point that button to the sort function of the gridview. Thank you for the help,

How to sort # column in GridView (asp.net) ?


I have a GridView with a column that contains rows numbers. somethign like below :

1    |
2    |
3    |
.     |
.     |
.     |
11  |
12  |
13  |
.    |
.    |
.    |

I've added it to the GridView with the following code :

<asp:TemplateField HeaderText="#" SortExpression="#">
<%# Container.DataItemIndex + 1 %>

Now I wanna sort GridView with this column, I've done it by reading this thread. But when I sort the GridView by # column, it sorts the GridView like below :

1   |
11 |
12 |
.   |
.   |
.   |
21 |
22 |
23 |
.   |
.   |
.   |

How can I fix it

Sort Gridview with jquery


hi to all plz tell me,

how to sort gridview using jquery???

thanks in advance

Gridview does not sort on gvClaimDetails_Sorting

i have placed the code inside and found out that it does not sort after clicking the clickable header column in gridview.

  <asp:BoundField HeaderText="Date" DataField="datecreatedname" SortExpression="datecreatedname"
                                    <HeaderStyle  ForeColor="White"></HeaderStyle>


protected void gvClaimDetails_Sorting(object sender, GridViewSortEventArgs e)
        DataTable m_DataTable = gvClaimDetails.DataSource as DataTable;

        if (m_DataTable != null)
            DataView m_DataView = new DataView(m_DataTable);
            m_DataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql2(e.SortDirection);

            gvClaimDetails.DataSource = m_DataView;



    private string ConvertSortDirectionToSql2(SortDirection sortDirection)

Gridview tooltip and sort expression


I am having trouble getting my column header to sort the column after adding a headertemplate that allows the column header to show a tooltip.

Here is the code:

<asp:TemplateField HeaderText="MiddleName" SortExpression="MiddleName">
                    <asp:Label ID="Label201" runat="server" ToolTip="Middle Name" Text="Middle Name"></asp:Label>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("MiddleName") %>'></asp:Label>

WPF: how to make a GridView only sort on one column as ascending order?


We use GridView inside the Listview. We need the grid view alwasy sort on one column ascending order and not allow to click column header to change it.

Is there a easy way to achieve it in Xaml?

If not possible, how to achieve it in code behind?



gridview sort on changed cell text


I have a gridview that is being databound by an SQLdatasource and on  RowDataBound i am doing a calculation and then setting


e.Row.cells(17).text = Result


However, when I go to sort it on the website it sorts it based off the previous values.


Cell(17) from the datasource reads in all 1's. The numbers I change it to range from 0 to 15. So when I click sort nothing changes because it sees it as all 1's.

How can i make the gridivew sort off the changed text values?

Why when I click on title header to sort a GridView, some data disappear?


 Hi there

When I click on my GridView header "End Time" all the rows which contain the string "N/A" disappear.
Here is my code:

protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
FillDataGridView_JobHistory(ViewState["DateBegin"].ToString(), ViewState["DateEnd"].ToString());

Note: GetSortDirection returns "endTime ASC" or "endTime DESC" and save in ViewState:

ViewState["SortDirection_JobHistory"] = sortDirection;
ViewState["SortExpression_JobHistory"] = column;

Then I fill the DataAdapter and use the ViewState info to sort it:

protected void FillDataGridView_JobHistory(string dateBegin, string dateEnd)
string query = "dbo.GetJobHistory";
DataTable dt = new DataTable();
SqlConnection myConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);

SqlCommand cmd = new SqlCommand(query, myConnection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@dateBegin", SqlDbType.DateTime));
cmd.Parameters.Add(new SqlParameter("@dateEnd", SqlDbTyp

Use jQuery to paging and sort asp.net GridView at client side



   I would like to know if there were any Plugin of jQuery to do both paging and sort for Asp.net Gridview control at client side?

    Thank you,



Sort not working in gridview after changing headings


Hi all,

I'm developing a bilingual web site, and therefore, I'm changing the text of the headings and the select buttons of my gridviews. I just discovered that if I set some code like below in the rowdatabound event of the gridview, the "linkbuttonish" headings get replaced by unclickable text. Thus, sorting the gridview is suddenly not working. What should I write instead?

If Session("lang") = "en" Then
            If e.Row.RowType = DataControlRowType.Header Then
                e.Row.Cells("name").Text = "Given name"
             End If
        End If

I guess that I should write something else than "e.Row.Cells("name").Text" to still have the click-behaviour, but I don't know what.

The same question arises for the select linkbutton in a cell of the gridview's datarows.

Thanks a lot!


sort gridview with ObjectContainerDataSource



I have a simple DTO object:?

Public Class TransactionDTO
    Public Property IDTransaction As Integer
    Public Property Subject As String
    Public Property StartDate As DateTime 
    Public Property EndDate As Nullable(of DateTime)
End Class


I bind this dto to a gridview with a ?ObjectContainerDataSource and try to sort to EndDate but I always receive the following error:??

Property 'EndDate' does not implement IComparable


Is there a workaround for this?

Inplace Edit in GridView

In this article I am presenting a different way to look at EditItemTemplate, using which user navigation for editing is reduced and . it looks very different (see screeen shots) then regular implementation of GridView.

GridView Confirmation Box Using JQuery BlockUI in asp.net

JQuery is the most popular JavaScript library. One of the main reasons for its fame is the plugin architecture. Anyone can create a separate library that leverage the JQuery library. In this article we are going to look at the BlockUI library which is used to block the user screen while performing a task.

Textbox Editable GridView in ASP.NET

This article describes how I made a fully editable gridview in C# using ASP.NET 2. The idea is that the GridView looks and works like an Excel spreadsheet. You see all the cells in the table, and you can edit any of the cells you like, and they are automatically updated (i.e. saved to the database). This is very often how people expect data tables to work in web pages, and I've often seen people clicking in vain on un-editable gridview cells, somehow expecting to edit them, and not understanding that they need to click an Edit button at the end of the row.

Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality

In my previous examples, I have demonstrated on how to add dynamic rows in GridView control with TextBoxes and how to save the values into the database. Now, seems that most of the developers are asking if how to add a delete functionality with it. So in this example, I'm going to show on how to delete a certain row in the dynamic GridView with TextBoxes.

AJAX GridView Checkbox Select All in ASP.Net

In the previous article GridView Select Multiple Rows you learnt the multiple row selection in GridView control. Here you will get the C# code sample to select all the gridview rows using single checkbox server control. You can highlight the selected rows with different color and Font state Bold. If you want to use the selection values on different ASP.Net
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend