Groups | Blog | Home
all groups > dotnet ado.net > february 2004 >

dotnet ado.net : How can I get the data form DataView with sorted order?


jennifer
2/7/2004 7:36:05 PM
Hi

I have some problem to get the data from data view
I created one table and sort one column in the dataview

DataTable myTable = new DataTable()
....Create Columns and add Record Values.

DataView myView = new DataView(myTable)
myView.Sort = "Price"

for(int i=0; i<myView.Table.Rows.Count; i++

myPrice = myView[i]["Price"]
....


When I check "myPrice" in every loop, it's not sorted order
How can I get the data with sorted data by "Price"

Thank yo
jennifer
2/7/2004 9:46:05 PM
Willia
Thank you for your answer

yes. I could gusss that I was looking at the actual table which is not sorted

Then, How can I get the data with sorted order
I really need some help this

William Ryan eMVP
2/8/2004 12:08:10 AM
Jennifer:

You are iterating through the datatable, not the view. If you called sort
on the view, it's sorted in the view, but it doesn't affect the position in
the underlying table, just like you can create a SQL view with sort order
specificied and if you look at the view, it's sorted but if you look at the
actual table, it isn't.
[quoted text, click to view]

Bernie Yaeger
2/8/2004 2:05:29 AM
Hi Jennifer,

Set the datatable in the same sort order:
dsretailer.Tables(0).DefaultView.Sort("price")

HTH,

Bernie Yaeger



[quoted text, click to view]

Bernie Yaeger
2/8/2004 2:06:55 AM
Hi Jennifer,

My syntax was incorrect: this is correct in vb .net:
dsretailer.Tables(0).DefaultView.Sort = "price"

HTH,

Bernie

[quoted text, click to view]

Miha Markic [MVP C#]
2/8/2004 9:40:59 AM
HI Bernie,

Just a note: this won't actually sort the table.
Just its DefaultView which is used when table is directly bound to consumer.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

[quoted text, click to view]

Cor
2/8/2004 11:15:22 AM
Hi Jennifer,

Can you change the following sentence and try it again.
I think that you than get what you want.
[quoted text, click to view]
for(int i=0; i<myView.Count; i++)

I hope this helps?

Cor

William Ryan eMVP
2/9/2004 2:26:50 PM
Here's an example of how to reference the sorted data:

private void btnOk_Click(object sender, System.EventArgs e)

{

dv.Sort = "Facility_Initials";

for(int i = 0; i < dv.Table.Rows.Count-1; i++)

{

tb1.Text +="\r\n";

tb1.Text += dv[i][1];


}

//this.DialogResult = DialogResult.OK;

}

private void btnCancel_Click(object sender, System.EventArgs e)

{

this.DialogResult = DialogResult.Cancel;

}


DataTable dt = new DataTable();

DataView dv = new DataView();

private void Form2_Load(object sender, System.EventArgs e)

{

da.Fill(dt);

foreach(DataRow dr in dt.Rows)

{ tb.Text +="\r\n";

tb.Text += dr[1].ToString() ;


}

[quoted text, click to view]

AddThis Social Bookmark Button