Home / Community & Support / Knowledge Base / RadControls for WinForms / GridView / Custom sorting by DisplayMember on ComboBox/Lookup column

Custom sorting by DisplayMember on ComboBox/Lookup column

Article Info

Rating: 3

Article information

Article relates to

 RadControls for WinForms Q3 2008 SP2

Created by

 Martin Vassilev

Last modified

 Feb. 19, 2008

Last modified by

 Martin Vassilev


HOW-TO
Implement custom sorting by DisplayMember instead of the default ValueMember

DESCRIPTION
Currently, RadGridView supports header click sorting that sorts data based on the cell values. This behavior is not suitable when you want to sort a ComboBox/Lookup column based on its displayed content instead their values.

SOLUTION
You could use custom sorting to change this default behavior. You only have to set CustomDataOperation property of your combo/lookup column to Sort and to subscribe for RadGridView's CustomSort event where you should implement your sort logic.

Here is some example code:
 
VB.NET
Private Sub Form1_Load(ByVal sender As ObjectByVal e As EventArgs)   
    Dim comboCol As New GridViewComboBoxColumn()   
      
    comboCol.DataSource = Me.productsBindingSource   
    'this.nwindDataSet.Products;   
    comboCol.ValueMember = "ProductID"   
    comboCol.DisplayMember = "ProductName"   
    comboCol.FieldName = "ProductID"   
    comboCol.HeaderText = "Product Name"   
    comboCol.Width = 250   
      
    Me.radGridView1.Columns.Insert(2, comboCol)   
    comboCol.CustomDataOperation = CustomDataOperation.Sorting   
End Sub   
 
Private Sub radGridView1_CustomSorting(ByVal sender As ObjectByVal e As GridViewCustomSortingEventArgs)   
    'Find corespondent value   
    Dim tblRow1 As ComboBoxSorting.DataSources.NwindDataSet.ProductsRow = Me.nwindDataSet.Products.FindByProductID(CInt(e.CellValue1))   
    Dim tblRow2 As ComboBoxSorting.DataSources.NwindDataSet.ProductsRow = Me.nwindDataSet.Products.FindByProductID(CInt(e.CellValue2))   
      
    Dim value1 As Object = [String].Empty   
    Dim value2 As Object = [String].Empty   
      
    If tblRow1 IsNot Nothing Then   
        value1 = tblRow1("ProductName")   
    End If   
      
    If tblRow2 IsNot Nothing Then   
        value2 = tblRow2("ProductName")   
    End If   
      
    If e.Column.SortOrder = RadSortOrder.Ascending Then   
        e.SortResult = DirectCast(value1, IComparable).CompareTo(value2)   
    Else   
        e.SortResult = DirectCast(value1, IComparable).CompareTo(value2) * (-1)   
    End If   
End Sub  


C#
private void Form1_Load(object sender, EventArgs e)     
{     
    GridViewComboBoxColumn comboCol = new GridViewComboBoxColumn();     
    
    comboCol.DataSource = this.productsBindingSource; //this.nwindDataSet.Products;     
    comboCol.ValueMember = "ProductID";     
    comboCol.DisplayMember = "ProductName";     
    comboCol.FieldName = "ProductID";     
    comboCol.HeaderText = "Product Name";     
    comboCol.Width = 250;     
         
    this.radGridView1.Columns.Insert(2, comboCol);     
    comboCol.CustomDataOperation = CustomDataOperation.Sorting;     
}     
    
private void radGridView1_CustomSorting(object sender, GridViewCustomSortingEventArgs e)     
{     
    //Find corespondent value     
    ComboBoxSorting.DataSources.NwindDataSet.ProductsRow tblRow1 =     
        this.nwindDataSet.Products.FindByProductID((int)e.CellValue1);     
    ComboBoxSorting.DataSources.NwindDataSet.ProductsRow tblRow2 =     
        this.nwindDataSet.Products.FindByProductID((int)e.CellValue2);     
    
    object value1 = String.Empty;  
    object value2 = String.Empty;  
 
    if (tblRow1 != null)  
    {  
        value1 = tblRow1["ProductName"];     
    }  
 
    if (tblRow2 != null)  
    {  
        value2 = tblRow2["ProductName"];     
    }  
    
    if (e.Column.SortOrder == RadSortOrder.Ascending)     
    {     
        e.SortResult = ((IComparable)value1).CompareTo(value2);     
    }                 
    else                 
    {                     
        e.SortResult = ((IComparable)value1).CompareTo(value2) * (-1);                 
    }     
}   

Comments

If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.