varios una totales total tabla suma puedo powerapps por ordenar manualmente ejemplos dinamica datos criterios como c# sorting datatable

c# - una - ordenar tabla dinamica varios criterios



Ordenando filas en una tabla de datos (12)

TL; DR

utilice tableObject.Select(queryExpression, sortOrderExpression) para seleccionar datos de forma ordenada

Ejemplo completo

Ejemplo de trabajo completo: se puede probar en una aplicación de consola :

using System; using System.Data; namespace A { class Program { static void Main(string[] args) { DataTable table = new DataTable("Orders"); table.Columns.Add("OrderID", typeof(Int32)); table.Columns.Add("OrderQuantity", typeof(Int32)); table.Columns.Add("CompanyName", typeof(string)); table.Columns.Add("Date", typeof(DateTime)); DataRow newRow = table.NewRow(); newRow["OrderID"] = 1; newRow["OrderQuantity"] = 3; newRow["CompanyName"] = "NewCompanyName"; newRow["Date"] = "1979, 1, 31"; // Add the row to the rows collection. table.Rows.Add(newRow); DataRow newRow2 = table.NewRow(); newRow2["OrderID"] = 2; newRow2["OrderQuantity"] = 2; newRow2["CompanyName"] = "NewCompanyName1"; table.Rows.Add(newRow2); DataRow newRow3 = table.NewRow(); newRow3["OrderID"] = 3; newRow3["OrderQuantity"] = 2; newRow3["CompanyName"] = "NewCompanyName2"; table.Rows.Add(newRow3); DataRow[] foundRows; Console.WriteLine("Original table''s CompanyNames"); Console.WriteLine("************************************"); foundRows = table.Select(); // Print column 0 of each returned row. for (int i = 0; i < foundRows.Length; i++) Console.WriteLine(foundRows[i][2]); // Presuming the DataTable has a column named Date. string expression = "Date = ''1/31/1979'' or OrderID = 2"; // string expression = "OrderQuantity = 2 and OrderID = 2"; // Sort descending by column named CompanyName. string sortOrder = "CompanyName ASC"; Console.WriteLine("/nCompanyNames data for Date = ''1/31/1979'' or OrderID = 2, sorted CompanyName ASC"); Console.WriteLine("************************************"); // Use the Select method to find all rows matching the filter. foundRows = table.Select(expression, sortOrder); // Print column 0 of each returned row. for (int i = 0; i < foundRows.Length; i++) Console.WriteLine(foundRows[i][2]); Console.ReadKey(); } } }

Salida

Tenemos dos columnas en una DataTable , así:

COL1 COL2 Abc 5 Def 8 Ghi 3

Estamos tratando de ordenar esta datatable de COL2 en función de COL2 en orden decreciente.

COL1 COL2 ghi 8 abc 4 def 3 jkl 1

Probamos esto:

ft.DefaultView.Sort = "occr desc"; ft = ft.DefaultView.ToTable(true);

pero, sin usar un DataView , queremos ordenar el DataTable sí mismo, no el DataView .


¿ Select(filterExpression, sortOrder) método Select(filterExpression, sortOrder) en DataTable? Vea here para un ejemplo. Tenga en cuenta que este método no ordenará la tabla de datos en su lugar, si eso es lo que está buscando, sino que devolverá una matriz ordenada de filas sin utilizar una vista de datos.


//Espero que esto te ayudará..

DataTable table = new DataTable(); //DataRow[] rowArray = dataTable.Select(); table = dataTable.Clone(); for (int i = dataTable.Rows.Count - 1; i >= 0; i--) { table.ImportRow(dataTable.Rows[i]); } return table;


Esto te ayudara...

DataTable dt = new DataTable(); dt.DefaultView.Sort = "Column_name desc"; dt = dt.DefaultView.ToTable();


Me temo que no puede hacer fácilmente una especie de DataTable en el lugar, como parece que quiere hacer.

Lo que puede hacer es crear una nueva DataTable desde un DataView que cree desde su DataTable original. Aplique las clases y / o filtros que desee en DataView y luego cree una nueva DataTable de DataView utilizando el método DataView.ToTable :

DataView dv = ft.DefaultView; dv.Sort = "occr desc"; DataTable sortedDT = dv.ToTable();


O bien, si puede usar DataGridView , puede simplemente llamar a Sort(column, direction) :

namespace Sorter { using System; using System.ComponentModel; using System.Windows.Forms; public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.dataGridView1.Rows.Add("Abc", 5); this.dataGridView1.Rows.Add("Def", 8); this.dataGridView1.Rows.Add("Ghi", 3); this.dataGridView1.Sort(this.dataGridView1.Columns[1], ListSortDirection.Ascending); } } }

Que le daría el resultado deseado:


Resulta que hay un caso especial donde esto se puede lograr. El truco está en construir DataTable, recoger todas las filas en una lista, ordenarlas y luego agregarlas. Este caso acaba de aparecer aquí.


Su uso simple. Seleccione la función.

DataRow[] foundRows=table.Select("Date = ''1/31/1979'' or OrderID = 2", "CompanyName ASC"); DataTable dt = foundRows.CopyToDataTable();

Y está hecho ...... Happy Coding


Tal vez lo siguiente puede ayudar:

DataRow[] dataRows = table.Select().OrderBy(u => u["EmailId"]).ToArray();

Aquí, puede usar otras consultas de expresión Lambda también.


prueba esto:

DataTable DT = new DataTable(); DataTable sortedDT = DT; sortedDT.Clear(); foreach (DataRow row in DT.Select("", "DiffTotal desc")) { sortedDT.NewRow(); sortedDT.Rows.Add(row); } DT = sortedDT;


Hay 2 formas de ordenar datos

1) ordenar solo los datos y rellenar la cuadrícula:

DataGridView datagridview1 = new DataGridView(); // for show data DataTable dt1 = new DataTable(); // have data DataTable dt2 = new DataTable(); // temp data table DataRow[] dra = dt1.Select("", "ID DESC"); if (dra.Length > 0) dt2 = dra.CopyToDataTable(); datagridview1.DataSource = dt2;

2) ordenar la vista predeterminada que es de ordenar con encabezado de columna de cuadrícula:

DataGridView datagridview1 = new DataGridView(); // for show data DataTable dt1 = new DataTable(); // have data dt1.DefaultView.Sort = "ID DESC"; datagridview1.DataSource = dt1;


table.DefaultView.Sort = "[occr] DESC";