EFileTahi-A Posted October 10, 2005 Posted October 10, 2005 (edited) 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: I'm using the sort thing on some independent DataTable that haves no connection with my MySQL DataTable. Edited October 10, 2005 by EFileTahi-A Quote
*Experts* Nerseus Posted October 10, 2005 *Experts* Posted October 10, 2005 What you have should be fine. How are you verifying your results - how do you know it's not sorting? If you're using a grid, be aware that sometimes grids have their own sorting. I use DevExpress's XtraGrid control and it ignores the sort on a DataView. Also, make sure you're binding to the DataView property, not the DataTable. If you're 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
EFileTahi-A Posted October 10, 2005 Author Posted October 10, 2005 I'm 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. I'm using a "For Loop" to show the data..." dtUniqueProps.DefaultView.Sort = "[props] asc"; for (int i = 0; i < dtUniqueProps.Rows.Count; i++) { MessageBox.Show(dtUniqueProps.Rows[i]["props"].ToString()); } Quote
EFileTahi-A Posted October 11, 2005 Author Posted October 11, 2005 (edited) Ok, I made this small project to do the sort thing but it does not work.DataTableSort.zip Edited October 11, 2005 by PlausiblyDamp Quote
pendragon Posted October 11, 2005 Posted October 11, 2005 You need to use the DataView and DataRowView to see it sorted. eg 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()); } [/Code] You can also set the rowfilter, sort and version of data you want when setting up the DataView. Quote
EFileTahi-A Posted October 11, 2005 Author Posted October 11, 2005 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? Quote
pendragon Posted October 11, 2005 Posted October 11, 2005 (edited) Hi EFileTahi-A Must admit never used the table.dataview.sort method before maybe this only works when data binding. Anyway the following works. 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()); } [/Code] You can also replace the first two lines of code with [Code] DataView dv = new DataView(this.dtTemp, "", "Items ASC", DataViewRowState.CurrentRows); [/Code] Edited October 11, 2005 by pendragon Quote
EFileTahi-A Posted October 11, 2005 Author Posted October 11, 2005 (edited) 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 can't perform a sort in directly in DataTable. Why do I have to create a DataView in order to sort it? I mean, this way I'm sorting DataView not the DataTable... Edited October 11, 2005 by EFileTahi-A Quote
*Experts* Nerseus Posted October 11, 2005 *Experts* Posted October 11, 2005 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. It's very confusing as you can't 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"): 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: 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 you'd typically do, but anyway: 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
EFileTahi-A Posted October 12, 2005 Author Posted October 12, 2005 Thank you nerseus for your time for writing such high quality/big post. All my doubts have been cleared. Thank you once more. :) Quote
pendragon Posted October 12, 2005 Posted October 12, 2005 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 don't 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: Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.