5 Answers, 1 is accepted
Thank you for writing.
The None sort option is quite often used, especially in scenarios where you have your have pre-sorted. However, one possible way to skip it is to override the Sort method of the header cell. Here is how to do that:
protected
override
void
OnLoad(EventArgs e)
{
base
.OnLoad(e);
AddGridSimple();
radGridView1.CreateCell += radGridView1_CreateCell;
}
void
radGridView1_CreateCell(
object
sender, GridViewCreateCellEventArgs e)
{
if
(e.CellType ==
typeof
(GridHeaderCellElement))
{
e.CellType =
typeof
(MyHeaderCell);
}
}
class
MyHeaderCell : GridHeaderCellElement
{
public
MyHeaderCell(GridViewColumn column, GridRowElement row) :
base
(column,row)
{
}
public
override
void
Sort(RadSortOrder sortOrder)
{
if
(sortOrder == RadSortOrder.None)
{
sortOrder = RadSortOrder.Ascending;
}
base
.Sort(sortOrder);
}
protected
override
Type ThemeEffectiveType
{
get
{
return
typeof
(GridHeaderCellElement);
}
}
}
I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.
Regards,
Stefan
Telerik
See What's Next in App Development. Register for TelerikNEXT.
I want the None option, but I do not have it. I am on version 2013 Q3. Was it put in after? or am I doing something wrong? If it was put in after, is there a way to have this functionality in my version?
Thanks,
This functionality was added in Q2 2014. In your version to add it you need to create customer header row behavior. Here is an example:
RadGridView radGridView1 =
new
RadGridView();
public
Form1()
{
InitializeComponent();
this
.Controls.Add(radGridView1);
radGridView1.Dock = DockStyle.Fill;
DataTable table =
new
DataTable();
table.Columns.Add(
"col1"
);
table.Columns.Add(
"col2"
);
table.Rows.Add(
"value1"
,
"value1"
);
table.Rows.Add(
"value2"
,
"value2"
);
radGridView1.DataSource = table;
BaseGridBehavior gridBehavior = radGridView1.GridBehavior
as
BaseGridBehavior;
gridBehavior.UnregisterBehavior(
typeof
(GridViewTableHeaderRowInfo));
gridBehavior.RegisterBehavior(
typeof
(GridViewTableHeaderRowInfo),
new
MyHeaderRowBehavior());
}
class
MyHeaderRowBehavior : GridHeaderRowBehavior
{
GridCellElement mouseDownCellElement;
public
override
bool
OnMouseDown(MouseEventArgs e)
{
mouseDownCellElement =
this
.GridControl.ElementTree.GetElementAtPoint(e.Location)
as
GridCellElement;
return
base
.OnMouseDown(e);
}
public
override
bool
OnMouseUp(MouseEventArgs e)
{
GridHeaderCellElement cell =
this
.GridControl.ElementTree.GetElementAtPoint(e.Location)
as
GridHeaderCellElement;
if
(e.Button == MouseButtons.Left &&
cell !=
null
&&
cell == mouseDownCellElement)
{
RadSortOrder sortOrder = cell.SortOrder;
if
(sortOrder == RadSortOrder.None)
{
sortOrder = RadSortOrder.Ascending;
}
else
if
(sortOrder == RadSortOrder.Ascending)
{
sortOrder = RadSortOrder.Descending;
}
else
{
sortOrder = RadSortOrder.None;
}
cell.Sort(sortOrder);
return
true
;
}
else
{
return
base
.OnMouseUp(e);
}
}
}
I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.
Regards,
Stefan
Telerik
Thank you Stefan,
While this has added the functionality, it seems to have a small side effect:
When I resize a column, it is applying the sort to the column on the right. Meaning if I have 2 columns (1 and 2). When i drag the resize slider, it will then sort column 2.
It is not a big deal and I would be sacrifice this for the no sort. But I am curious if there is way around.
And I appreciate your help in this thread and in the others. You seem very knowledgeable on this control and great response time. It is making using this control a pleasure.
Indeed, this behavior occurs as the base functionality kicks in. Can you please try the following behavior and let me know how it works for you:
class
MyHeaderRowBehavior : GridHeaderRowBehavior
{
bool
sortedDescending =
false
;
public
override
bool
OnMouseUp(MouseEventArgs e)
{
RadSortOrder saveOrder = RadSortOrder.None;
GridHeaderCellElement cell =
this
.GridControl.ElementTree.GetElementAtPoint(e.Location)
as
GridHeaderCellElement;
if
(cell !=
null
)
{
saveOrder = cell.SortOrder;
}
bool
res =
base
.OnMouseUp(e);
if
(saveOrder != cell.SortOrder)
{
if
(cell.SortOrder == RadSortOrder.Ascending)
{
if
(sortedDescending)
{
cell.Sort(RadSortOrder.None);
sortedDescending =
false
;
}
else
{
return
res;
}
}
else
if
(cell.SortOrder == RadSortOrder.Descending)
{
sortedDescending =
true
;
return
res;
}
}
return
res;
}
}
I am looking forward to your reply.
Regards,
Stefan
Telerik