问题
I have a DataView, which has been sorted in some order. How can I retrieve the values using an index.
Something like this:
if(dv.rows[0]["name"]=="xxx")
{
--- do something ---
}
else
--- something else ---
回答1:
Try the following code
Move the sorted DataView to DataTable like
DataTable dt = dv.ToTable();
Then use
if (dt.Rows[0]["name"] == "xxx")
{
[...]
}
It will work.
回答2:
Did you try:
DataRowView rowView = dv[index];
回答3:
Rather than converting the whole thing back to a Table, you can work with the DataView directly:
To get a row from a DataView
, you use the Item
property, which returns a DataRowView
, and you can then call Item
to get at the cells, all of which can be shortened to:
// Returns object, so needs to be cast to your required type:
if ((string)dv[0]["CellName"] == "ABCD")
{
[...]
}
回答4:
I don't quite know if this is the answer you're looking for:
if (dv.Rows[0].Cells["CellName"].Value == "ABCD")
{
}
来源:https://stackoverflow.com/questions/700869/retrieving-rows-in-dataview-by-its-index