This is a migrated thread and some comments may be shown as answers.

Stopping user from sorting GridView but allowing sorting in code

3 Answers 353 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Grand River Rubber
Top achievements
Rank 2
Grand River Rubber asked on 17 Aug 2017, 05:39 PM

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

3 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 18 Aug 2017, 08:04 AM
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.
0
Grand River Rubber
Top achievements
Rank 2
answered on 21 Aug 2017, 05:04 PM

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!

0
Dimitar
Telerik team
answered on 22 Aug 2017, 05:57 AM
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.
Tags
GridView
Asked by
Grand River Rubber
Top achievements
Rank 2
Answers by
Dimitar
Telerik team
Grand River Rubber
Top achievements
Rank 2
Share this question
or