How can I get the data form DataView with sorted order?

G

Guest

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["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
 
W

William Ryan eMVP

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.
 
G

Guest

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

Thank you.
 
B

Bernie Yaeger

Hi Jennifer,

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

HTH,

Bernie Yaeger
 
B

Bernie Yaeger

Hi Jennifer,

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

HTH,

Bernie
 
M

Miha Markic [MVP C#]

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.
 
C

Cor

Hi Jennifer,

Can you change the following sentence and try it again.
I think that you than get what you want.
for(int i=0; i<myView.Table.Rows.Count; i++)
for(int i=0; i<myView.Count; i++)

I hope this helps?

Cor
 
W

William Ryan eMVP

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[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() ;


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top