Sorting and filtering data in a DataSet

J

Jesper Stocholm

I have a DataSet that I need to do some sorting and filtering on.
Basically I need to filter on the value in the 3rd column being equal to
'r' and sorting in descending order on column 20.

I have seen, that I can put the DataSet (or its table) through a DataView
and apply sorting and filtering to it - like

private void CreateHtmlTable(XmlTextWriter w, DataSet reimbursementData_)
{

DataView dataView = new DataView();
dataView = reimbursementData_.Tables[0].DefaultView;
dataView.RowFilter = "Type = 'r'";
dataView.Sort = "DebtorID";
foreach (DataRow row in reimbursementData_.Tables[0].Rows)
{
// incomplete code below
int debtorId = 0;
if (debtorId != Convert.ToInt32(row[20]))
{
// write end table row definition </tr>...
}
else
{
// write start table row definition ...
}
}
}

My question is this: In the code above I loop through the rows of the
table of the original dataset - but is this correct? Will the original
dataset be affected by the sorting and filtering applied by the DataView?

If this is not the case - how do I loop through the DataView? The class
does not seem to have any methods available for this.

Thanks,

:blush:)
 
J

Jon Skeet [C# MVP]

Jesper Stocholm said:
I have a DataSet that I need to do some sorting and filtering on.
Basically I need to filter on the value in the 3rd column being equal to
'r' and sorting in descending order on column 20.

I have seen, that I can put the DataSet (or its table) through a DataView
and apply sorting and filtering to it - like

private void CreateHtmlTable(XmlTextWriter w, DataSet reimbursementData_)
{

DataView dataView = new DataView();
dataView = reimbursementData_.Tables[0].DefaultView;
dataView.RowFilter = "Type = 'r'";
dataView.Sort = "DebtorID";
foreach (DataRow row in reimbursementData_.Tables[0].Rows)
{
// incomplete code below
int debtorId = 0;
if (debtorId != Convert.ToInt32(row[20]))
{
// write end table row definition </tr>...
}
else
{
// write start table row definition ...
}
}
}

My question is this: In the code above I loop through the rows of the
table of the original dataset - but is this correct? Will the original
dataset be affected by the sorting and filtering applied by the DataView?
No.

If this is not the case - how do I loop through the DataView? The class
does not seem to have any methods available for this.

Instead of

foreach (DataRow row in reimbursementData_.Tables[0].Rows)

use

foreach (DataRowView row in dataView)
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Jesper,

First of all a DataView is related with a DataTable, not a DataSet, I think
it's important to note it.

You can think of a dataview basically what it does is keep a new index to
the datatable. something like this:
// JUST TO GET you an idea of how it works
class DataView{
DataTable datasource;
// This array is used to keep track of the indices of each row in the
datasource.
int[] indices_in_source;

//NOTE THAT DataView return a DataViewRow, no DataRow as shown here.
public DataRow this[int index_in_view]
{
get
{
// see how you translate the element in the position X in the
dataview to the element in the position Y in the datatable
return datasource.Rows[ indices_in_source[index_in_view] ];
}

}

}
My question is this: In the code above I loop through the rows of the
table of the original dataset - but is this correct? Will the original
dataset be affected by the sorting and filtering applied by the DataView?

The original DataTable will not be affected in the sense that it will not
change the order of the rows.
You can change the DataTable's row contain from the DataView though.
If this is not the case - how do I loop through the DataView? The class
does not seem to have any methods available for this.

The default indexer will do the trick as jon's explained.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Jesper Stocholm said:
I have a DataSet that I need to do some sorting and filtering on.
Basically I need to filter on the value in the 3rd column being equal to
'r' and sorting in descending order on column 20.

I have seen, that I can put the DataSet (or its table) through a DataView
and apply sorting and filtering to it - like

private void CreateHtmlTable(XmlTextWriter w, DataSet reimbursementData_)
{

DataView dataView = new DataView();
dataView = reimbursementData_.Tables[0].DefaultView;
dataView.RowFilter = "Type = 'r'";
dataView.Sort = "DebtorID";
foreach (DataRow row in reimbursementData_.Tables[0].Rows)
{
// incomplete code below
int debtorId = 0;
if (debtorId != Convert.ToInt32(row[20]))
{
// write end table row definition </tr>...
}
else
{
// write start table row definition ...
}
}
}

My question is this: In the code above I loop through the rows of the
table of the original dataset - but is this correct? Will the original
dataset be affected by the sorting and filtering applied by the DataView?

If this is not the case - how do I loop through the DataView? The class
does not seem to have any methods available for this.

Thanks,

:blush:)

--
Jesper Stocholm http://stocholm.dk

Programmer's code comment:
//It probably makes more sense when you're stoned.
 

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