vs2003: identify first row in DataRow[] - c#

what is the best way to identify first row in the following code?
foreach(DataRow row in myrows)
{
if (first row )
{
...do this...
}
else
{
....process other than first rows..
}
}

You can use a boolean flag for this:
bool isFirst = true;
foreach(DataRow row in myrows)
{
if (isFirst)
{
isFirst = false;
...do this...
}
else
{
....process other than first rows..
}
}

you could just use a for loop instead
for(int i = 0; i < myrows.Count; i++)
{
DataRow row = myrows[i];
if (i == 0) { }
else { }
{

Maybe something like this?
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Index == 0)
{
//...
}
else
{
//...
}
}

Use a int to loop through the collection.
for (int i =0; i < myDataTable.Rows.Count;i++)
{
if (i ==0)
{
//first row code here
}
else
{
//other rows here
}
}

First convert the DataRow to DataRowView:
How Can Convert DataRow to DataRowView in c#
and after that:
foreach (DataRowView rowview in DataView)
{
if (DataRowView.Table.Rows.IndexOf(rowview.Row) == 0)
{
// bla, bla, bla...
}
}

Related

DataTable.SetField appears slow

From performance analysis, this is very slow:
public static DataTable ReplaceNulls(DataTable table, double replacementValue = 0)
{
int r = -1;
foreach (DataRow row in table.Rows)
{
r++;
foreach (DataColumn col in table.Columns.Cast<DataColumn>().Skip(1))
{
if ((object)row[col] == DBNull.Value)
{
row.SetField(col, replacementValue);
}
}
}
return table;
}
The issue is the SetField line. Can anyone suggest a faster approach?

null value in cellformatting

I have a datagrid called dgMember_Grade that takes its data from a stored procedure.
One of its columns represent a date called vacationStart.
I want to color the rows based on cell value, but it gives me an error on the foreach line:
Cannot convert type 'char' to 'System.Windows.Forms.DataGridViewCell'
I tried the code:
foreach (DataGridViewRow Myrow in dgMember_Grade.Rows)
{
foreach (DataGridViewCell cell in Myrow.Cells[26].Value.ToString()) // error on foreach
{
if (cell != null)
{
DateTime date = DateTime.Now;
DateTime expiredate = DateTime.Parse(Myrow.Cells[26].Value.ToString());
if (expiredate < date)
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
else
{
Myrow.DefaultCellStyle.BackColor = Color.Green;
}
}
}
}
Here is the code you can use:
foreach (DataGridViewRow Myrow in dgMember_Grade.Rows)
{
var cellValue = Myrow.Cells[26].Value;
if (cellValue == null || cellValue == DBNull.Value)
continue;
DateTime expiredate = Convert.ToDateTime(cellValue);
if (expiredate < DateTime.Now)
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
else
{
Myrow.DefaultCellStyle.BackColor = Color.Green;
}
}
Note:
You don't need 2 loops, You can simply use a loop betwen Rows and apply what you need based on Myrow.Cells[26].Value
You can use events like CellFormatting or CellPainting or RowPrePaint to apply formatting.
If you already know an index of the cell.
May be you are trying to do this:
foreach (DataGridViewRow Myrow in dgMember_Grade.Rows)
{
if (Myrow.Cells[26].Value == null) {
continue;
}
DateTime expiredate = DateTime.Parse(Myrow.Cells[26].Value.ToString());
if (expiredate < DateTime.Now)
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
else
{
Myrow.DefaultCellStyle.BackColor = Color.Green;
}
}

Filter Criteria % like % following Code

i use this code for focus row which match text in search text box.now how can i use % like % criteria for this code
foreach (DataGridViewRow row in GdvDetails.Rows)
{
var cellValue = row.Cells["Bunch Component"].Value;
if (cellValue != null && cellValue.ToString() == txtSearch.Text)
{
GdvDetails.Rows[row.Index].DefaultCellStyle.BackColor = Color.Yellow;
}
else
{
GdvDetails.Rows[row.Index].DefaultCellStyle.BackColor = Color.Empty;
}
}
try this
foreach (DataGridViewRow row in GdvDetails.Rows)
{
var cellValue = row.Cells["Bunch Component"].Value;
if (cellValue != null)
{
if ( cellValue.ToString().Contains(txtSearch.Text))
{
GdvDetails.Rows[row.Index].DefaultCellStyle.BackColor = Color.Yellow;
}
else
{
GdvDetails.Rows[row.Index].DefaultCellStyle.BackColor = Color.Empty;
}
}
}

Delete row in a DataGridView C#

I tried to loop through my dataGridView1 and remove rows which don't satisfy the condition as following:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0)
{
dataGridView1.Rows.Remove(row); //error: Uncommitted new row cannot be deleted.
}
}
But I got this error:
Uncommitted new row cannot be deleted.
I can manage if the code also VB.NET.
Don't use foreach in this case, the looped collection may be modified and leads to unpredicted result, sometimes throws exception like collection was modified (encountered mainly in LINQ), use for instead:
for(int i = dataGridView1.RowCount-1; i >= 0; i--){
var row = dataGridView1.Rows[i];
if (!row.IsNewRow&&!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0){
dataGridView1.Rows.Remove(row);
}
}
Note that we have to loop from the largest index to 0.
Try
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!(row.Cells.OfType<DataGridViewCell>().All(c=>c.Value == null))
{
if (!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0)
{
dataGridView1.Rows.Remove(row);
}
}
}
try with putting following condition:
foreach(DataGridViewRow row in dataGridView1.Rows)
{
if(!row.IsNewRow)
{
if (!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0)
{
dataGridView1.Rows.Remove(row);
}
}
}

C# Datagridview last index out of bound

hi guys im new here =)
i would need your help in a minimal troubble case, to save time for me =)
The problem is about that, i want to have datagridview Cells Values and
save the values to an array.
Up to now it works fine, but it just reads the first row cell and after
that process it just tells that the index is out of bound, but i dont understand why.
foreach (DataGridViewRow row in dataGridView1.Rows) //wo soll er sich aufhalten -> zeilen
{
**if (row.Cells[i].Value == null)**
{
MessageBox.Show("This row is empty");
break;
}
if (row.Cells[i].Value != null)
{
UnsortArray[i] = row.Cells[i].Value.ToString();
MessageBox.Show(UnsortArray[i]);
++i;
}
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i=0; i<= dataGridView1.Columns.Count; i++)
{
**if (row.Cells[i].Value == null)**
{
MessageBox.Show("This row is empty");
}
if (row.Cells[i].Value != null)
{
UnsortArray[i] = row.Cells[i].Value.ToString();
MessageBox.Show(UnsortArray[i]);
}
}
}
also, I don't understand why would you want to do messageBox.show inside a loop. It should be out side the loop.
foreach (DataGridViewRow row in dataGridView1.Rows) //wo soll er sich aufhalten -> zeilen
{
if (row.Cells[i].Value == null)
MessageBox.Show("This row is empty")
else
{
UnsortArray[i] = row.Cells[i].Value.ToString();
MessageBox.Show(UnsortArray[i]);
// ++i; dont need to increment if you want to read only specific column
}
}
Try something like following code>>
for(int i=0;i<dataGridView1.Rows.Count;i++)
{
if (dataGridView1.Rows[i].cells[i].Value==null)**
{
MessageBox.Show("This row is empty");
break;
}
else
{
UnsortArray[i] = dataGridView1.Rows[i].cells[i].Value.ToString();
MessageBox.Show(UnsortArray[i]);
}
}
Use different Index variable for UnSortArray (let say j). i is a index of cell which value you want to store in UnSortArray
CODE
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[i].Value == null)
{
MessageBox.Show("This row is empty")
}
else
{
UnsortArray[j] = row.Cells[i].Value.ToString();
MessageBox.Show(UnsortArray[j]);
++j;
}
}

Categories