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

How to sort columns according to their names?

I am using GridDataBoundGrid. The columns are dynamically added at each time and should be displayed aftering sorting. Thanks~ Rebecca

2 Replies

AD Administrator Syncfusion Team December 12, 2004 08:49 AM UTC

Here are two ways you can do it. The first uses the DataView associated with the datasource. It is more flexible in that you can control the sort order and sort multiple columns since you are directly setting the DataView.Sort property. The second method using the grid.Binder.Sort call to do the sort.
//Using the DataView.Sort
CurrencyManager cm = grid.BindingContext[grid.DataSource, grid.DataMember] as CurrencyManager;
if(cm != null)
	DataView dv = cm.List as DataView;
	if(dv != null)
		dv.Sort = "someColName desc";

//using the grid.Binder.Sort 
int colIndex = grid.Binder.NameToColIndex("someColName");
int field = grid.Binder.ColIndexToField(colIndex);

AD Administrator Syncfusion Team December 12, 2004 11:22 AM UTC

I may have missunderstood your question. The above code sorts the record in the DataTable by the values in the column you can choose by name. If you actually want to display the columns from the datatable by the alpha-order of their column names, then one way to do this is to get the names (which you can do from the DataTable.Columns collection), order these names, and then create GridBoundColumns and add them to the grid.GridBoundColumns collection in the sorted order.

Live Chat Icon For mobile
Up arrow icon