Sort items in datatable

I have a datatable with a single column that contains names. I want to load combobox with datatable so that the names are in alphabetical order, for example: the name starts with a. middle name begins with b. How can I sort the data in datatable.Can anyone help?

+4
source share
5 answers

Use DataView:

DataTable dt; //comes from somewhere... DataView dv = new DataView(dt) dv.Sort = "Name ASC"; foreach(DataRowView drv in dv) //.... 
+13
source

Method 1

  // orderby "FistName" column EnumerableRowCollection<DataRow> query = from order in datatable.AsEnumerable() orderby datatable.Field<string>("FirstName") select datatable; DataView view = query.AsDataView(); // bind to your combobox combobox.DataSource = view; combobox.DataBind() 

Method 2 : when using DataSets

  DataTable datatable = dataSet.Tables["yourDataTable"]; DataView view = datatable .AsDataView(); view.Sort = "FirstName desc, LastName desc"; combobox.DataSource = view; combobox.DataBind(); 

Link: Sort with DataView (LINQ to DataSet)

+4
source

Here is the answer you are looking for.

 DataTable MyDataTable; const string SortByClause = "[SomeCol] ASC"; MyDataTable.DefaultView.Sort = SortByClause ; 
+3
source

use a typed dataset to create data that can be used in a precision data type for example, I created dsAppointment

  DsAppointment dsAppointmentTmp = new DsAppointment(); DsAppointment dsAppointment = new DsAppointment(); //add all appointment dsAppointmentTmp.Appointment.AddAppointmentRow(name,start,end,body) //use select(filter,sort(name of columns) DataRow[] rows1 = dsAppointmentTmp.Tables[0].Select(string.Empty, dsAppointmentTmp.Tables[0].Columns[1].ToString()); foreach (DataRow thisRow in rows1) { dsAppointment.Tables[0].Rows.Add(thisRow.ItemArray); } 

// returns the sort dsAppointment return dsAppointment;

0
source

Source: https://habr.com/ru/post/1301782/


All Articles