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 3935644
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:51:13+00:00 2026-05-19T23:51:13+00:00

I have a gridview with several columns, 3 of which I’d like to sort.

  • 0

I have a gridview with several columns, 3 of which I’d like to sort.
The source for the data is held in the session.

protected void MyGridHistorySort(object sender, GridViewSortEventArgs e)
{
    var TheColumn = (e.SortExpression).ToString();

    TheDataHistory = (List<ViewDataHistoryModel>)Session["SessionDataHistory"];

    var test = "data.DataDate";

    var NewDataSource = from data in TheDataHistory
                    orderby test
                    select data;

    MyGridHistory.DataSource = NewDataSource;
    MyGridHistory.DataBind();

DataDate is a valid column in the list but the orderby statement doesn’t work. Ideally, I’d like it to sort with the variable TheColumn by writing something like test = “data.”+TheColum; and then add a sort direction based on a boolean. I looked at the OrderBy extension method NewDataSource.OrderBy(test); but that doesn’t work either.

What am I missing to make my code work?

Thanks.

  • 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-19T23:51:14+00:00Added an answer on May 19, 2026 at 11:51 pm

    It looks like you need to have

    var NewDataSource = from data in TheDataHistory
                        orderby data.DataDate
                        select data;
    

    Since the orderby clause takes a property name. The clause doesn’t parse the string to determine what to order by like Dynamic Queryable does.

    Or, in the case of sorting on multiple fields, I would use

    var NewDataSource = from data in TheDataHistory
                        select data;
    
    switch(fieldToSortOn)
    {
      case "field1":
        NewDataSource = NewDataSource.OrderBy(x => x.Field1);
        break;
      case "field2":
        NewDataSource = NewDataSource.OrderBy(x => x.Field2);
        break;
    
      ...
    
    }
    

    If you are really bent on just using a string to sort this, then I would totally recommend the Dynamic Queryable library. I’ve been using it and it’s perfect for what I’ve been doing lately (with some added support for DateTime? objects)

    Then you can do things like

    NewDataSource = NewDataSource.DynamicOrderBy("ID DESC, Name DESC, Price ASC");
    

    Love this library!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have ListView that uses a GridView to display several columns of data. Two
Background: I have a ListView / GridView with several columns. In some situations, only
I have a custom GridView like control made of a Grid panel with several
Have a gridview control which will show X amount of rows. I want to
I have a GridView like this( main.xml ): <?xml version=1.0 encoding=utf-8?> <GridView xmlns:android=http://schemas.android.com/apk/res/android android:id=@+id/gridView1
I have a gridview setup that gets its data from a SQL database stored
I have the following XAML layout: <ScrollViewer> <Grid> <GridView/> // several gridviews with other
I have a Winform DataGridView , which has a number of predefined columns. Now
Currently I'm programming something in asp.net. I have a gridview of 69ish columns and
I have a GridView that displays several dozen rows worth of a custom layout,

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.