C# vs2003:标识DataRow[]中的第一行

C# vs2003:标识DataRow[]中的第一行,c#,visual-studio-2003,datarow,C#,Visual Studio 2003,Datarow,识别以下代码中第一行的最佳方法是什么 foreach(DataRow row in myrows) { if (first row ) { ...do this... } else { ....process other than first rows.. } } 您可以使用for循环来代替 for(int i = 0; i < myrows.Count; i++) { DataRow row = myrows[i]; if (i == 0) { } else

识别以下代码中第一行的最佳方法是什么

foreach(DataRow row in myrows)
{

if (first row )
{
...do this...
}
else
{
....process other than first rows..
}
}

您可以使用for循环来代替

for(int i = 0; i < myrows.Count; i++) 
{
    DataRow row = myrows[i];
    if (i == 0) { }
    else { }
{
for(int i=0;i
您可以为此使用布尔标志:

bool isFirst = true;

foreach(DataRow row in myrows)
{
    if (isFirst)
    {
        isFirst = false;
        ...do this...
    }
    else
    {
        ....process other than first rows..
    }
}

也许是这样的

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.Index == 0)
        {
            //...
        }
        else
        {
            //...
        }
    }

使用int循环遍历集合

for (int i =0; i < myDataTable.Rows.Count;i++)
{
    if (i ==0)
    {
       //first row code here
    }
    else
    {
       //other rows here
    }
}
for(int i=0;i
首先将数据行转换为数据行视图:

在那之后:

    foreach (DataRowView rowview in DataView)
{
    if (DataRowView.Table.Rows.IndexOf(rowview.Row) == 0)
    {
        // bla, bla, bla... 
    }
}

嗯…@Adam,我相信你回答这个问题有很好的理由。我很好奇为什么使用一个布尔值来检查行索引==0更有效?谢谢!-@Javier:你是说调用
Array.IndexOf(myrows,row)吗==0
?如果是这样,那么是的,这绝对是更有效的。如果您想知道索引,那么Hunter使用
for
循环的解决方案将更为可取。如果您指的是
行.index
,那么这是其父
数据表
中的索引,而不是他描述的数组中的索引。他表示他有一个
DataRow[]
,而不是
DataTable
。表中的行索引与其在任意数组中的索引之间没有必要的相关性。