我工作的Web應用程序,我必須在dataTable中查找行位置。請幫我弄清楚這一點。 下面是Windows應用程序的代碼示例如何查找行數據表
pos = paesiBindingSource.Find("ISOCode", country);
什麼,我想在我的webapp做的是這裏
pos = Convert.ToInt32(PerfDBCountryTable.Select(country, "ISOCode").ToString());
我工作的Web應用程序,我必須在dataTable中查找行位置。請幫我弄清楚這一點。 下面是Windows應用程序的代碼示例如何查找行數據表
pos = paesiBindingSource.Find("ISOCode", country);
什麼,我想在我的webapp做的是這裏
pos = Convert.ToInt32(PerfDBCountryTable.Select(country, "ISOCode").ToString());
有一個返回DataRow
表中的索引沒有直接的財產。
但是你可以使用DataRowCollection.IndexOf
:
DataRow[] rows = PerfDBCountryTable.Select("Country = " + country, "ISOCode");
if(rows.Length > 0)
{
int index = PerfDBCountryTable.Rows.IndexOf(rows[0]);
}
另一種方法是使用Linq-To-DataTable
:
int firstMatchingRowIndex = PerfDBCountryTable.AsEnumerable()
.Select((row, index) => new { row, index })
.Where(x => x.row.Field<string>("Country") == country)
.Select(x => x.index)
.DefaultIfEmpty(-1)
.First();
int rowNumber = 0;
var pos = PerfDBCountryTable.Select(country, "ISOCode");
rowNumber = PerfDBCountryTable.Rows.IndexOf(pos);
你能解釋一下爲什麼這個工作? –
我試圖使用LINQ解決方案,它的工作完美感謝 – Dev