Retrieving rows in DataView by its index

我们两清 提交于 2019-12-11 06:25:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!