First, to sort the DataTable in C #, you can use the DefaultView sort method. First get the DefaultView of the DataTable, then set the DataView's Sort property, and finally use the TOtable method of the view to export the ordered DataView to a DataTable.
The code is as follows:
DataTable dt = new DataTable ();
Dt. Columns.Add ("ID", typeof (int));
Dt. Columns.Add ("Name", typeof (String));
Dt. Rows.Add (new object[] {n, "Lwolf"});
Dt. Rows.Add (new object[] {n, "kkkkk"});
Dt. Rows.Add (new object[] {n, "Jim"});
Dt. Rows.Add (New object[] {1, "test"});
DataTable dtcopy = dt. Copy ();Comments
DataView dv = dt. DefaultView;
DV. Sort = "ID";
dtcopy = dv. ToTable ();
Dt. Defaultview.sort = "Age ASC";
DT = dt. Defaultview.totable ();
Second, the specific sort example
protected void Page_Load (object sender, EventArgs e)
{
DataTable dt = new DataTable ();
Dt. Columns.Add ("Name");
Dt. Columns.Add ("Age");//Because it is a string, it is not sorted
Dt. Rows.Add ("Xiaoming", "21");
Dt. Rows.Add ("Xiao Zhang", "10");
Dt. Rows.Add ("Little Red", "9");
Dt. Rows.Add ("Xiao Wei", "7");
Dt. Rows.Add ("Mei", "3");
Dt. Defaultview.sort = "Age ASC";
DT = dt. Defaultview.totable ();
foreach (DataRow s in dt. Rows)
{
Response.Write (s["age"). ToString () + "--" + s["Name"]. ToString () + "<br/>");
}
Response.Write ("------------------1----------------<br/>");
#region Method 1: Make the age 2-bit and then sort it, but there should not be 0 in practice (for reference only)
for (int i = 0; i < dt. Rows.Count; i++)
{
Dt. Rows[i]["Age" = dt. Rows[i]["Age"]. ToString (). PadLeft (2, ' 0 ');
}
Dt. Defaultview.sort = "Age ASC";
DT = dt. Defaultview.totable ();
foreach (DataRow s in dt. Rows)
{
Response.Write (s["age"). ToString () + "--" + s["Name"]. ToString () + "<br/>");
}
#endregion
Response.Write ("------------------2----------------<br/>");
#region Method 2: Create a new DataTable, change the age type to int type
DataTable dtnew = dt. Clone ();
Dtnew.columns["Age"]. DataType = typeof (int);//Specify Age as int type
foreach (DataRow s in dt. Rows)
{
Dtnew.importrow (s);//import old data
}
DtNew.DefaultView.Sort = "Age ASC";
Dtnew = DtNew.DefaultView.ToTable ();
foreach (DataRow s in dtnew.rows)
{
Response.Write (s["age"). ToString () + "--" + s["Name"]. ToString () + "<br/>");
}
#endregion
Response.Write ("-----------------3-----------------<br/>");
#region Method 3: Add a column that is used primarily for sorting
Dt. Columns.Add ("Agelength", typeof (int), "Len"), or//When the column is added, the DataTable column data is generated
Dt. Defaultview.sort = "Agelength,age ASC";
DT = dt. Defaultview.totable ();
foreach (DataRow s in dt. Rows)
{
Response.Write (s["age"). ToString () + "--" + s["Name"]. ToString () + "<br/>");
}
#endregion
Response.Write ("-----------------4-----------------<br/>");
#region Method 4: Use LINQ to convert a DataTable to a collection, and then call the collection's own sorting method to sort
foreach (DataRow s in dt. Rows.cast<datarow> (). The by-(r = Int. Parse (r["age"). ToString ())))
{
Response.Write (s["age"). ToString () + "--" + s["Name"]. ToString () + "<br/>");
}
#endregion
}
Three
The Select () method of the DataTable returns an array of DataRow,
Dt31 = Dt30 in your code. Select ("Invoice number = '" +a+ ""); obviously Dt31 is a DataTable type, and the assignment number is followed by a DataRow array.
Analysis of the landlord's meaning, may want to Dt30 in the invoice number a data, a new DataTable as the data source of the GridView it?
If so, you can use the following code:
String A;
A =dt30. ROWS[0][1]. ToString ();
DataView DataView = Dt30. defaultview;//Define a default view of DataView as Dt30
DataView.RowFilter = "Invoice number = '" +a+ "'"; Filtering on DataView
This.dataGridView5.DataSource = dataview;//Filtered dataView do the data source
C # Sort the DataTable