DataTable dt; DataView dv = new DataView(dt);dv.Sort = "EmpID Asc";dt= dv.toTable(); //Bind the data table to DataGridViewDataGridView1.DataSource = dt;DataGridView1.Refresh();
private static void SortDataTable(DataTable dt, string sortCol) { //Take a Clone of the DataTableDataTable newDT = dt.Clone(); int rowCount = dt.Rows.Count; //Rows Count//Using Select Method to sort the RowsDataRow[] SortedRows = dt.Select(null, sortCol); // Sort with Column name for (int i = 0 ; i < rowCount; i++) { object[] arr = new object[dt.Columns.Count]; for (int j = 0; j < dt.Columns.Count; j++) { arr[j]=SortedRows[i][j]; } //Add the Sorted Row DataRow data_row = newDT.NewRow(); data_row.ItemArray=arr; newDT.Rows.Add(data_row); } //clear the incoming dt.Rows.Clear(); for(int i = 0; i < newDT.Rows.Count; i++) { object[] arr = new object[dt.Columns.Count]; for (int j = 0; j < dt.Columns.Count; j++) { arr[j]=newDT.Rows[i][j]; } DataRow data_row = dt.NewRow(); data_row.ItemArray = arr; dt.Rows.Add(data_row); } dt.AcceptChanges();//Bind the Sorted Table to DataGridViewDataGridView1.DataSource = dt;DataGridView1.Refresh();}