How do you sort column data and direction?

I need to use a DataTable in memory based on the column and direction that come from the GridView. The function should look like this:

public static DataTable resort(DataTable dt, string colName, string direction) { DataTable dtOut = null; .... } 

I need help filling out this feature. I think I can use the Select statement, but I'm not sure how to do this. I can’t click on "Comments" because of this browser, but you can show me in place either the new DataTable solution, or one. For people showing me pointers, please, I need a coded function similar to the one that was prototyped.

What about:

 // ds.Tables[0].DefaultView.Sort="au_fname DESC"; public static void Resort(ref DataTable dt, string colName, string direction) { string sortExpression = string.Format("{0} {1}", colName, direction); dt.DefaultView.Sort = sortExpression; } 
+65
c # datatable
Feb 15 2018-11-15T00:
source share
6 answers

I assume that "direction" is "ASC" or "DESC" and dt contains a column called "colName"

 public static DataTable resort(DataTable dt, string colName, string direction) { DataTable dtOut = null; dt.DefaultView.Sort = colName + " " + direction; dtOut = dt.DefaultView.ToTable(); return dtOut; } 

OR without creating dtOut

 public static DataTable resort(DataTable dt, string colName, string direction) { dt.DefaultView.Sort = colName + " " + direction; dt = dt.DefaultView.ToTable(); return dt; } 
+101
Dec 25
source share

If you have only one DataView, you can sort it:

 table.DefaultView.Sort = "columnName asc"; 

Not tried, but I think you can do it with any number of DataViews if you are referencing the correct one.

+60
Aug 04 '11 at 15:09
source share

Actually the same problem. It was easy for me:

Adding data to Datatable and sorting:

 dt.DefaultView.Sort = "columnname"; dt = dt.DefaultView.ToTable(); 
+11
Nov 10 '15 at 13:54
source share

There is an overloaded Select method in DataTables that you can do for this. See here: http://msdn.microsoft.com/en-us/library/way3dy9w.aspx

But the val return for calling Select is not a DataTable, but an array of RowData objects. If you want to return a DataTable from your function, you will have to build it from scratch based on this data array. Here is a message that addresses and provides a sample for both problems: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/157a4a0f-1324-4301-9725-3def95de2bf2/

+7
Feb 15 '11 at 3:40
source share

Create a DataView . You cannot sort a DataTable directly, but you can create a DataView from a DataTable and sort it.

Creation: http://msdn.microsoft.com/en-us/library/hy5b8exc.aspx

Sort: http://msdn.microsoft.com/en-us/library/13wb36xf.aspx

The following code example creates a view showing all products where the number of units in stock is less than or equal to the reordering level, sorted first by vendor ID and then by product name.

DataView prodView = new DataView(prodDS.Tables["Products"], "UnitsInStock <= ReorderLevel", "SupplierID, ProductName", DataViewRowState.CurrentRows);

+4
Feb 15 2018-11-15T00:
source share

If you want to sort more than one direction

  public static void sortOutputTable(ref DataTable output) { DataView dv = output.DefaultView; dv.Sort = "specialCode ASC, otherCode DESC"; DataTable sortedDT = dv.ToTable(); output = sortedDT; } 
+2
Jan 24 '17 at 16:50
source share



All Articles