We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

GridControl Sorting ComboBox column

I have a non databound grid control where one of the columns is a combobox. I have sorting enabled on the grid control and when I sort the column that is the combobox it sorts by the valuemember not the displaymember. Is there anyway to specify the sorting for this or override it for this specific column? I am using version 1.6.1.5 currently and don''t think I will be able to update on this release.

6 Replies

Administrator February 9, 2004 09:30 PM UTC

I am not sure when it was added, but the SortByColumn now has an overload that takes an IComparer object that lets you do custom sorting. Below is a sample that shows how you can use this interface to sort a foreign key combo column by the Display Member instead of the value member. SortwithComparer_6414.zip

Administrator February 10, 2004 01:04 PM UTC

Well it is not the release I am on. I get the no overload method takes 4 parameters message. I am running 1.6.1.5.

Administrator February 10, 2004 01:56 PM UTC

If you cannot use the updated grid, then I think you would have to write the sort routine yourself. Then instead of calling this.gridControl1.Data.SortByColumn(col,(ListSortDirection)this.gridControl1[0, col].Tag, 0, cc); you would call your routine. In your sort routine, you can use whatever technique you want to sort things. It would take a little coding, but can be done. Depending on the size of the grids involved, you might just be able to use grid.Rows.MoveRange to move the rows around. More efficiently, you could use an index array, and just move the indexes during the sorting, and then rearrange the grid once after the sorting is complete. There are many other possibilties as well.

Administrator February 10, 2004 02:43 PM UTC

Well call me lazy but I upgraded to the 1.6.1.8 release which has the Comparer ability in it. However the sample you sent me makes the assumption that the row number matches the column called id, which in my case is not true. So I don''t see how you can get the text value of the column using this sample (the text in the GridStyleInfo reports as the valuemember in text not the displaymember).

Administrator February 10, 2004 03:35 PM UTC

If your database is not set up to easily access the other fields in the selected item, then you will have to add code to find the right selected item. Below is some code that uses a dataview to do this, but there are other options as well. (You could even loop through the DataTable looking for the proper row. Or, if you set the id up as a primary key, you could use Datatable.Find.))
public class ComparerClass : IComparer
{
	private DataTable dt;
	private DataView dv;
	public ComparerClass(DataTable dt)
	{
		this.dt = dt;
		this.dv = new DataView(dt, "", "", DataViewRowState.CurrentRows);
	}

	public int Compare(object x, object y)
	{
		GridStyleInfo styleX = new GridStyleInfo(x as GridStyleInfoStore);
		GridStyleInfo styleY = new GridStyleInfo(y as GridStyleInfoStore);
		string sx = "";
		string sy = "";

		dv.RowFilter = string.Format("[id] = {0}", styleX.CellValue);
		if(dv.Count > 0)
		{
			sx = dv[0]["color"].ToString();
		}

		dv.RowFilter = string.Format("[id] = {0}", styleY.CellValue);
		if(dv.Count > 0)
		{
			sy = dv[0]["color"].ToString();
		}
		return sx.CompareTo(sy);
	}
}

Administrator February 10, 2004 04:32 PM UTC

Thanks. That was exactly what I ended up doing, it is not very quick so I put in an hour glass during the sort, but it is good enough.
Loader.
Live Chat Icon For mobile
Up arrow icon