VS2003: identificare prima riga DataRow []
-
20-09-2019 - |
Domanda
che cosa è il modo migliore per identificare prima fila nel codice seguente?
foreach(DataRow row in myrows)
{
if (first row )
{
...do this...
}
else
{
....process other than first rows..
}
}
Soluzione
È possibile utilizzare un flag booleano per questo:
bool isFirst = true;
foreach(DataRow row in myrows)
{
if (isFirst)
{
isFirst = false;
...do this...
}
else
{
....process other than first rows..
}
}
Altri suggerimenti
si potrebbe utilizzare un ciclo for invece
for(int i = 0; i < myrows.Count; i++)
{
DataRow row = myrows[i];
if (i == 0) { }
else { }
{
Forse qualcosa di simile?
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Index == 0)
{
//...
}
else
{
//...
}
}
Usa un int per scorrere la collezione.
for (int i =0; i < myDataTable.Rows.Count;i++)
{
if (i ==0)
{
//first row code here
}
else
{
//other rows here
}
}
Per prima convertire il DataRow a DataRowView:
Come può Convertire DataRow a DataRowView in C #
e dopo che:
foreach (DataRowView rowview in DataView)
{
if (DataRowView.Table.Rows.IndexOf(rowview.Row) == 0)
{
// bla, bla, bla...
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow