Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6548755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:00:31+00:00 2026-05-25T12:00:31+00:00

I have a gridview which I want to sort when any of its column

  • 0

I have a gridview which I want to sort when any of its column header is clicked. There is a DataTable that is built on runtime and assigned to the gridview to populate data. Here is the DataTable and Gridview:

DataTable dtMedication = new DataTable();
dtClientMedications.Columns.Add("Id");
dtClientMedications.Columns.Add("BrandName");
dtClientMedications.Columns.Add("GenericName");
dtClientMedications.Columns.Add("Dosage");
dtClientMedications.Columns.Add("Physician");
dtClientMedications.Columns.Add("DatePrescribed");
dtClientMedications.Columns.Add("Status");
dtClientMedications.Columns.Add("ClientMedicationDataId");

<asp:GridView ID="gdvMainList" runat="server" AutoGenerateColumns="False"
                            SkinID="PagedGridView" onrowcommand="gdvMainList_RowCommand" 
                            DataKeyNames="Id" onsorting="gdvMainList_Sorting">
                            <PagerStyle CssClass="gridpager" HorizontalAlign="Right" />
                            <Columns>
                                <ucc:CommandFieldControl HeaderText="Actions" ShowDeleteButton="true" ButtonType="Image"
                                    DeleteImageUrl="~/App_Themes/Default/images/delete.png" ShowEditButton="true"
                                    EditImageUrl="~/App_Themes/Default/images/edit.png" DeleteConfirmationText="Are you sure you want to delete?">
                                    <ItemStyle HorizontalAlign="Center" Width="60px" />
                                </ucc:CommandFieldControl>
                                <asp:BoundField DataField="BrandName" HeaderText="Brand Name" />
                                <asp:BoundField DataField="GenericName" HeaderText="Generic Name" />
                                <asp:BoundField DataField="Dosage" HeaderText="Dosage" />
                                <asp:BoundField DataField="Physician" HeaderText="Physician" />
                                <asp:BoundField DataField="DatePrescribed" HeaderText="Date Prescribed" />
                                <asp:BoundField DataField="Status" HeaderText="Status" />
                                <asp:TemplateField HeaderText="">
                                    <ItemStyle CssClass="HiddenCol" Width="0px" />
                                    <HeaderStyle CssClass="HiddenCol" />
                                    <ItemTemplate>
                                        <asp:HiddenField ID="hdfClientMedicationDataId" runat="server" Value='<%#Bind("ClientMedicationDataId") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <EmptyDataTemplate>
                                <div class="divEmptyGrid">
                                    --- No Medication Exists ---
                                </div>
                            </EmptyDataTemplate>
                        </asp:GridView>

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:00:32+00:00Added an answer on May 25, 2026 at 12:00 pm

    First step to set AllowSorting property of GridView to true then add SortExpression property for each column you would to use for sorting:

    SortExpression property indicates the expression that should be
    used to sort the data when that field’s sorting header link is clicked

    Let’s consider BoundField from your code above, I have added a SortExpression property with value set to BrandName which means that when the column header for BrandName will be clicked column ‘BrandName’ in your DataTable will be used to sort the data:

    Now in gdvMainList_Sorting event you have to rebind the grid to sorted data:

    protected void gdvMainList_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
    {
        //Using DataView for sorting DataTable's data
        DataView view = dtMedication.DefaultView;
        view.Sort = String.Format("{0} {1}", e.SortExpression, GetSortingDirection());
        gdvMainList.DataSource = view;
        gdvMainList.DataBind();
    }
    

    If you have noticed I have used getSortingDirection(), which a method that returns either ‘ASC’ or ‘DESC’ for sorting data in either ascending or descending order, respectively.

    protected string GetSortingDirection() 
    {
        if(ViewState["SortDirection"] == null)
            ViewState["SortDirection"] = "ASC";
        else if(ViewState["SortDirection"] == "ASC")
            ViewState["SortDirection"] = "DESC";
        else
            ViewState["SortDirection"] = "ASC";
    
        return ViewState["SortDirection"];
    }
    

    Some useful links:

    1. GridView sorting using VB.net
    2. Sorting and Paging tutorial
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GridView to which I bind a dataTable that I manually created.
I have a GridView and a column in GridView which has a linkButton that
I have a ListView (GridView) that I want to sort by 2 columns, so
I have a Gridview which is binded to a datatable that I created. The
i have a GridView (selectable) in which I want to generate a dynamic GridView
Have a gridview control which will show X amount of rows. I want to
want to have a Hyperlink-Button in a gridView in which I can display a
I have a gridview which I am binding through code behind, I want to
I have a gridview which is bound to a DataTable. When I try to
I have a gridview on which i want to apply quick-search plugin of jquery.I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.