Skip to main content

Sorting by column in DetailTable object

The following code can be used to sort by a specific column in the DetailTable object.

In the example, the rows on the DetailTable1 object are sorted by the value in the TextBox of Text1. All data in DetayMasa is first transferred to a DataTable, and ASC is sorted according to the text1 column on the DataTable. Afterwards, all rows on DetailTable1 are deleted and the sorted data in DataTable is added back to DetailTable1.

using System.Data; using System.Web.UI.WebControls;

DataTable data = new DataTable();

DataColumn text1 = new DataColumn("Text1", typeof(string));

DataColumn text2 = new DataColumn("Text2", typeof(string));

DataColumn list1 = new DataColumn("List1", typeof(string));

DataColumn liste1_Text = new DataColumn("Liste1_TEXT", typeof(string));

data. Columns.Add(text1);

data. Columns.Add(text2);

data. Columns.Add(list1);

data. Columns.Add(liste1_Text);

for (int i = 0; i < DetailTable1.CurrentRowCount; i++)

{

DataRow = newRow data. NewRow();

newRow["Text1"] = ((TextBox)DetailTable1.GetRowObject(i, "Text1")). Text;

newRow["Text2"] = ((TextBox)DetailTable1.GetRowObject(i, "Text2")). Text;

newRow["List1"] = ((DropDownList)DetailTable1.GetRowObject(i, "List1")). SelectedValue;

newRow["Liste1_TEXT"] = ((DropDownList)DetailTable1.GetRowObject(i, "List1")). SelectedItem.Text;

data. Rows.Add(newRow);

DataView = data. DefaultView;

dataview. sort = "text1 ASC";

DataTable dtSort = dataview. ToTable();

}

DetailTable1.CurrentRowCount = 0;

foreach (DataRow dr in dtSort.Rows)

{

((TextBox)DetailTable1.GetRowObject(DetailTable1.CurrentRowCount, "Text1")). Text = dr["Text1"]. ToString();

((TextBox)DetailTable1.GetRowObject(DetailTable1.CurrentRowCount, "Text2")). Text = dr["Text2"]. ToString();

((DropDownList)DetailTable1.GetRowObject(DetailTable1.CurrentRowCount, "List1")). SelectedValue = dr["List1"]. ToString();

((DropDownList)DetailTable1.GetRowObject(DetailTable1.CurrentRowCount, "List1")). SelectedItem.Text = dr["Liste1_TEXT"]. ToString();

DetailTable1.CurrentRowCount++;

}