Justin Ross
Justin Ross

Reputation: 392

ASP.NET Gridview case insensitive column sorting

I am implementing a gridview to handle a single sql server database table. When I click to sort a column, it does so by capital letters first (e.g. Test, Test2, Test3, test1, test2). Is there a way to manipulate the sorting so that it would return the results as expected (e.g. Test, test1, Test2, test2, Test3)?

Upvotes: 0

Views: 699

Answers (2)

Pankaj
Pankaj

Reputation: 695

Try following in .aspx file:

<asp:GridView ID="grd" runat="server">
</asp:GridView>

And add below in .aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();

    dt.Columns.Add("Subject", typeof(string));

    dt.Rows.Add("Test");
    dt.Rows.Add("Test2");
    dt.Rows.Add("Test3");
    dt.Rows.Add("test1");
    dt.Rows.Add("test2");

    dt.CaseSensitive = true; // this will allow case sensitive sorting
    dt.DefaultView.Sort = "Subject asc";

    grd.DataSource = dt;
    grd.DataBind();
}

For more details refer this link How to sort a DataView in a case-insensitive manner?

Please mark this answer useful if this solve your problem.

Upvotes: 1

Paritosh
Paritosh

Reputation: 4503

I think you have to use the Grid's Sorting event to do a custom sort: http://www.nullskull.com/a/866/aspnet--sorting-a-gridview-bound-to-a-custom-data-object.aspx

Otherwise if you don't want to do that, just have the database return another column with all lowercase text. Then in the gridView Column, where you have the current Test, Test2,etc...display both in there, have one be a hidden label and set the sort to that column. I haven't tried this but it might work, so in that's columns ItemTemplate have both output but only show one, then sort on the hidden one.

Upvotes: 0

Related Questions