Stopping user from sorting GridView but allowing sorting in code

4 posts, 1 answers
  1. Grand River Rubber
    Grand River Rubber avatar
    6 posts
    Member since:
    Oct 2012

    Posted 17 Aug 2017 Link to this post

    I've searched like crazy and just can't seem to find an answer to this. I have a RadGridView, which has a sort order set with a SortDescriptor. This works perfectly. However, I don't want the user to be able to change the sort order. I don't see an obvious way to achieve this.

    I tried the answer here: http://www.telerik.com/forums/radgridview-and-sort#HOkVZJ4q4UqUdzg5ZW-sgg in that I overrode the Header Cells. I don't have anything in OnMouseUp, just like the example, but sorting was still allowed to occur.

    I then tried overriding the Sort event, which worked, with a caveat. Here's the code I have now:

    private void ingredientsGridView_CreateCell(object sender, GridViewCreateCellEventArgs e)
    {
        if (e.CellType == typeof(GridHeaderCellElement))
        {
            e.CellType = typeof(MyHeaderCell);
        }
    }
     
     
    public class MyHeaderCell : GridHeaderCellElement
    {
        public MyHeaderCell(GridViewColumn column, GridRowElement row)
            : base(column, row)
        { }
     
        protected override Type ThemeEffectiveType
        {
            get { return typeof(GridHeaderCellElement); }
        }
     
        //protected override void OnMouseUp(MouseEventArgs e)
        //{
        //    //base.OnMouseUp(e);
        //    //MessageBox.Show("Nope");
        //}
     
        public override void Sort(RadSortOrder sortOrder)
        {
            //base.Sort(sortOrder);
        }
    }

     

    As I stated, this works, but the caveat is that the user still sees options to sort in the context menu, etc. Is there a better way to achieve what I want, allowing sorting in code but keeping the user from sorting?

    Thanks

  2. Answer
    Dimitar
    Admin
    Dimitar avatar
    2831 posts

    Posted 18 Aug 2017 Link to this post

    Hello Scott,

    You can disable the sorting for each column. There is no need to use custom cells in this case. Here is an example:
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        radGridView1.SortDescriptors.Add("Name", ListSortDirection.Ascending);
        foreach (var item in radGridView1.Columns)
        {
            item.AllowSort = false;
        }
    }

    Should you have any other questions do not hesitate to ask.

    Regards,
    Dimitar
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Grand River Rubber
    Grand River Rubber avatar
    6 posts
    Member since:
    Oct 2012

    Posted 21 Aug 2017 in reply to Dimitar Link to this post

    Dimitar,

    Thank you, that worked. I didn't think I could do that since I need to sort the columns in code, but I found it works if I sort first and then disable sorting per column. I also figured out how to hide the sort indicators from http://www.telerik.com/forums/sort-arrow-glyphs so everything works the way I want now. Thanks!

  4. Dimitar
    Admin
    Dimitar avatar
    2831 posts

    Posted 22 Aug 2017 Link to this post

    Hello Scott,

    I am glad that this is working fine now. Do not hesitate to contact us if you have other questions.

    Regards,
    Dimitar
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top