GridView sort only to specified fields

Posted on 2011-09-20
Last Modified: 2012-05-12

When I turn allow sorting to true, I get sort to all fields,
how can I make the sort option onlt to some fields and to others not?
Question by:nuritat
  • 3
  • 2
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36565843
By setting the DataGridViewColumn.SortMode property you can disable the ability for the user to sort on the given column. You can use Visual Studio to set this property by right-clicking on the DataGridView and choosing the Edit Columns option. Next select the column that you want to disable sorting for and set the SortMode property to NotSortable.


Author Comment

ID: 36565864
Thats what I was looking for, but unfortunatly I have only "SortExpression" element in property list and not sortMode

LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36565876
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36565884
Above link contains solution for datagridview  
  dgvCol.SortMode = DataGridViewColumnSortMode.NotSortable

Change it accordingly for Gridview.


Accepted Solution

chandrasekar1 earned 500 total points
ID: 36565925
i think you are talking about Gridview, if yes then check the below code

protected void gdvDataViewer_PreRender1(object sender, EventArgs e) 
    Style s = new Style(); 
    s.ForeColor = System.Drawing.Color.Red; 
    s.Width = 200; 
    if (gdvDataViewer.AllowSorting) 
        foreach (TableCell tc in gdvDataViewer.HeaderRow.Cells) 
            if (tc.HasControls()) 
                LinkButton lb = (LinkButton)tc.Controls[0]; 
                if (lb != null && lb.Text.Equals("CoumnNmae")) // your column to make unsortable 
                    tc.Attributes.Add("onclick","return false;"); 

Open in new window

ref link

Author Closing Comment

ID: 36566646
perfect solution for gridview that doesn't have SortModa as property


Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity 2015 Syncing Azure web app projects on two machines 2 26
IEnumerable<T> to a List<T> 8 40
Where is this file? 3 26
using CK editor in iframes 5 18
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question