C# - Sorting a column within a DataTable

EFileTahi-A

Well-known member
Joined
Aug 8, 2004
Messages
539
Location
Portugal / Barreiro
Ok, I tryed:

myDataTable.DefaultView.Sort = "column_name asc";
myDataTable.DefaultView.Sort = "[column_name] asc";

But nothing... No columns sorted... No errors...

Am I doing something wrong?

NOTE: Im using the sort thing on some independent DataTable that haves no connection with my MySQL DataTable.
 
Last edited by a moderator:
What you have should be fine. How are you verifying your results - how do you know its not sorting? If youre using a grid, be aware that sometimes grids have their own sorting. I use DevExpresss XtraGrid control and it ignores the sort on a DataView.

Also, make sure youre binding to the DataView property, not the DataTable. If youre doing looping, loop through the DataView and use the type DataRowView not a DataRow (which loops through the rows of the table and ignores the sorting/filtering).

-ner
 
Im not using any kind of grid... only some datatable object with only one column to hold some values, and this object is not binded to anything.

Im using a "For Loop" to show the data..."

Code:
dtUniqueProps.DefaultView.Sort = "[props] asc";

for (int i = 0; i < dtUniqueProps.Rows.Count; i++)
{
	MessageBox.Show(dtUniqueProps.Rows[i]["props"].ToString());
}
 
You need to use the DataView and DataRowView to see it sorted.

eg

Code:
dtUniqueProps.DefaultView.Sort = "[props] asc";

DataView dv = new DataView(dtUniqueProps);
DataRowView row;

for (int i = 0; i < dv.Count; i++)
{
             row = dv[i];
	MessageBox.Show(row["props"].ToString());
}

You can also set the rowfilter, sort and version of data you want when setting up the DataView.
 
Pendragon, my old buddy, I tryed it the way you told me, but, with no success... What Am I doing wrong?!... could you please download the project I previously attached and try to make it work?
 
Hi EFileTahi-A

Must admit never used the table.dataview.sort method before maybe this only works when data binding.

Anyway the following works.

Code:
	DataView dv = new DataView(this.dtTemp);
	dv.Sort = "Items ASC";
	DataRowView row;

	for (int i = 0; i < dv.Count; i++)
	{
		row = dv[i];
		this.listBox2.Items.Add(row[0].ToString());
	}

You can also replace the first two lines of code with

Code:
DataView dv = new DataView(this.dtTemp, "", "Items ASC", DataViewRowState.CurrentRows);
 
Last edited by a moderator:
Ahhh!!! Now it works! You have to perform a sort on the DataView instead the DataTable itself...

Thank you Pendragon!

Anyway, I wonder why I cant perform a sort in directly in DataTable. Why do I have to create a DataView in order to sort it? I mean, this way Im sorting DataView not the DataTable...
 
Last edited by a moderator:
The trouble is that you set the Sort on the DataView (every table has one, when you reference the DefaultView property) but you loop through the rows which reference back to the DataTable. You have to loop through the DataRowView objects, as I said, which belong to the DataView. Its very confusing as you cant get to an indexed list of the rows using the DataView but have to use a foreach (or GetEnumerator). This had me baffled for weeks when I first came across it.

The following will NOT work, but is more typical (changed your "for(int i...)" to "foreach"):
C#:
private void button1_Click(object sender, System.EventArgs e)
{
	this.listBox2.Items.Clear();

	dtTemp.DefaultView.Sort = "[Items] ASC";

	foreach(DataRow row in dtTemp.Rows)
	{
		this.listBox2.Items.Add(row[0].ToString());
	}

}

Now, if you change that to use the DataRowView, this WILL work:
C#:
private void button1_Click(object sender, System.EventArgs e)
{
	this.listBox2.Items.Clear();

	dtTemp.DefaultView.Sort = "[Items] ASC";

	foreach([b]DataRowView[/b] row in dtTemp.[b]DefaultView[/b])
	{
		this.listBox2.Items.Add(row[0].ToString());
	}

}

You could also use binding. In this code snippet, I set the binding in your button1, probably not what youd typically do, but anyway:
C#:
private void button1_Click(object sender, System.EventArgs e)
{
	dtTemp.DefaultView.Sort = "[Items] ASC";
	listBox2.DataSource = dtTemp.DefaultView;
	listBox2.DisplayMember = "Items";
	listBox2.ValueMember = "Items";
}
-ner
 
Thanks Nerseus, now I also understand what the defaultview option is used for and how it is accessed.

When I was learning ADO.NET (and am by no means an expert yet as I think has just been proved) I brought the Microsoft ADO.NET core reference book and I dont believe that this way of doing it is even mentioned, I wonder how many other things I am doing that can be done in a simpler way :confused:
 
Back
Top