Sorting dataset Columns

G

Guest

I have a dataset which am trying to sort by 3 specific columns. I am not
making any connection to the database to get the dataset. I have the raw
dataset already.
What i tried doing was to create a DataView and sort it by a column name.

mydataset.ReadXml(@sResponseXML);
DataView mydataview = new DataView();
mydataview = ds.Tables[0].DefaultView;
mydataview.Sort = "ColumnName";

Then I call a function Export() passing the dataset. The function loops thru
the dataset and writes the output as a csv file. So technically I cannot say
mydataview.Tables[0].Columns to loop thru the data view nor can I use the
foreach statement on a dataview.

I even tried sorting the dataset itself and passing it to the Export function
ds.Tables[0].DefaultView.Sort = "Country of Risk"; This doesnt work either.

Can someone give any ideas about how to handle this? I'd really appreciate.

Thanks
Deepa
 
C

Cor Ligthert

Deepa,

I once made a snippet, maybe you can use it.

\\\
DataView dv = new DataView(dt);
dv.Sort = "bla"; // sorts column bla
DataTable dtnew = dt.Clone();
foreach (DataRowView dvr in dv)
dtnew.ImportRow(dvr.Row);
dt.Clear();
dt = dtnew.Copy();
///

I hope this helps?

Cor
 
G

Guest

Cor
in the statement DataView dv = new DataView(dt);
is dt another data table object?
In my scenario am loading the dataset object using the ReadXml. Later am
looping thru the dataset.

-Deepa
 
C

Cor Ligthert

Deepa,

dt is the datatable you want to sort.
Something as ds.Tables[0]

As sample with a datagrid.
\\\
private void button1_Click(object sender, System.EventArgs e)
{
DataTable dt = (DataTable)(dataGrid1.DataSource);
dataGrid1.DataSource = null;
dataGrid1.DataSource = sortmytable(dt);
}
private DataTable sortmytable(DataTable dt )
{
DataView dv = new DataView(dt);
dv.Sort = "bla";
DataTable dtnew = dt.Clone();
foreach (DataRowView dvr in dv)
dtnew.ImportRow(dvr.Row);
return(dtnew.Copy());
}
//

I hope this helps

Cor
 

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

Similar Threads

sorting and paging 1
Thread Safety, dataset and dataview, ASP.NET 1
Sorting a dataset 8
DataSet Question 2
sorting gridview question 6
Unable to sort Dataview 11
DataGrid problem 1
Sorting a Strong Typed Dataset 3

Top