【发布时间】:2010-03-16 17:19:44
【问题描述】:
在以下代码中识别第一行的最佳方法是什么?
foreach(DataRow row in myrows)
{
if (first row )
{
...do this...
}
else
{
....process other than first rows..
}
}
【问题讨论】:
标签: c# visual-studio-2003 datarow
在以下代码中识别第一行的最佳方法是什么?
foreach(DataRow row in myrows)
{
if (first row )
{
...do this...
}
else
{
....process other than first rows..
}
}
【问题讨论】:
标签: c# visual-studio-2003 datarow
您可以为此使用布尔标志:
bool isFirst = true;
foreach(DataRow row in myrows)
{
if (isFirst)
{
isFirst = false;
...do this...
}
else
{
....process other than first rows..
}
}
【讨论】:
Array.IndexOf(myrows, row) == 0?如果是这样,那么是的,这绝对更有效。如果您想知道索引,那么 Hunter 使用 for 循环的解决方案会更可取。如果您指的是row.Index,那么这就是它在其父DataTable 中的索引,不是在他描述的数组中。
你可以使用 for 循环代替
for(int i = 0; i < myrows.Count; i++)
{
DataRow row = myrows[i];
if (i == 0) { }
else { }
{
【讨论】:
也许是这样的?
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Index == 0)
{
//...
}
else
{
//...
}
}
【讨论】:
DataRow[],而不是DataTable。表中行的索引与其在任意数组中的索引之间没有必要的关联。
使用 int 循环遍历集合。
for (int i =0; i < myDataTable.Rows.Count;i++)
{
if (i ==0)
{
//first row code here
}
else
{
//other rows here
}
}
【讨论】:
首先将DataRow转换为DataRowView:
How Can Convert DataRow to DataRowView in c#
然后:
foreach (DataRowView rowview in DataView)
{
if (DataRowView.Table.Rows.IndexOf(rowview.Row) == 0)
{
// bla, bla, bla...
}
}
【讨论】: