Sorting a DataTable without binding it to a data control

M

Mark Rae

Hi,

Sorry about this, but I'm having a brainstorm here. I'm returning a DataSet
from a SQL Server database via a stored procedure, and need to sort the data
by a dynamically chosen set of fields. The DataSet is then used for various
tasks, but is *not* used as the DataSource of e.g. a DataGrid. Once I've got
my DataSet object populated with data, I'm doing the following:

DataView objDV = mobjDS.Tables[0].DefaultView;
if(cmbGroupBy.SelectedValue.Substring(1, 1) == "S")
{
objDV.Sort = "Level0Name, IssuerName, IssueDesc ASC";
}

Then I'm walking through the DataView as follows:

foreach(DataRow objDR in objDV.Table.Rows)
{
// processing
}

Problem is that the data isn't sorted. I''m clearly missing something
totally obvious... Does populating a DataView's Sort property not actually
sort the data until the DataView is bound to a control...?

Any assistance gratefully received.

Mark
 
B

Bart Mermuys

Hi,

Mark Rae said:
Hi,

Sorry about this, but I'm having a brainstorm here. I'm returning a
DataSet from a SQL Server database via a stored procedure, and need to
sort the data by a dynamically chosen set of fields. The DataSet is then
used for various tasks, but is *not* used as the DataSource of e.g. a
DataGrid. Once I've got my DataSet object populated with data, I'm doing
the following:

DataView objDV = mobjDS.Tables[0].DefaultView;
if(cmbGroupBy.SelectedValue.Substring(1, 1) == "S")
{
objDV.Sort = "Level0Name, IssuerName, IssueDesc ASC";
}

Then I'm walking through the DataView as follows:

foreach(DataRow objDR in objDV.Table.Rows)
{
// processing
}

You're not enumerating the DataView, but the DataTable's Rows collection.

It should be like this:

foreach (DataRowView drv in objDV )
{
// processing
}

HTH,
Greetings
 
M

Mark Rae

You're not enumerating the DataView, but the DataTable's Rows collection.

It should be like this:

foreach (DataRowView drv in objDV )
{
// processing
}

ROTFLMAO! D'oh - sometimes you can't see the wood from the trees.

Thanks for the answer - of course, you are right :)
 

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