1.

Solve : C# Foreach statement not working on datagrid?

Answer»

I need help on a program I am currently creating as I am trying to use a foreach statement on a DataGridView & loop through the rows & cells. I have tried the dataview but this doesn’t seem to have the properties to count through cells. I have therefore tried to use a for each statement on a datagridview to loop through the rows, then the cells & count the cells. However, when I run the code & press the BUTTON to execute the loop, the code just bypasses the foreach statement & does not count the rows. I have looked through lots of examples but can’t seem to get this working and its really frustrating. Below is the code with the loop. I need help please

Code: [Select][

Dv = new DataView(Movset1.Tables[0]);
DataGridView Dgv1 = new DataGridView();
Dgv1.DataSource = this.Dv.Table;

int CellCount = 0;
foreach (DataGridViewRow dr in Dgv1.Rows)
{
foreach (DataGridViewCell dc in dr.Cells)
{
ROWCOUNT++;
}
}
MessageBox.Show(" there are " + CellCount + " Cells ");

/code]What is Rowcount ?
What gives it a start value?sorry, Rowcount should be cellCount & start value was nil. Ive corrected the code below

Code: [Select]Dv = new DataView(Movset1.Tables[0]);
DataGridView Dgv1 = new DataGridView();
Dgv1.DataSource = this.Dv.Table;

int CellCount = 0;
foreach (DataGridViewRow dr in Dgv1.Rows)
{
foreach (DataGridViewCell dc in dr.Cells)
{
CellCount ++;
}
}
MessageBox.Show(" there are " + CellCount + " Cells ");



Ive see the same code used in another example and it's worked for them, so am fluxed why the code doesnt work for me, as the cell loop just gets bypassedQuote from: markyjj on June 11, 2013, 04:45:06 PM

sorry, Rowcount should be cellCount & start value was nil. Ive corrected the code below
nil isn't a value....

Have you tried setting a breakpoint and inspecting locals?Can you show us the ORIGINAL code.
It looks like there should be another statement. You have only one statement inside a compound for loop. I think C# requires both loops to have statements. Maybe I am wrong. It should not hurt to add a null statement.Quote from: Geek-9pm on June 11, 2013, 07:29:47 PM
Can you show us the original code.
You have only one statement inside a compound for loop. I think C# requires both loops to have statements. Maybe I am wrong. It should not hurt to add a null statement.
What are you on about?Quote from: BC_Programmer on June 11, 2013, 08:31:50 PM
What are you on about?
I am wrong. Sorry.Below is the full code for that section when the update button is pressed. The rows show in the table in DV when I check the debugger but not in Dgv1 when the DV is set as the datasource for the Datagridview (Dgv1)


Code: [Select] private void UpdateSavebtn_Click_1(object sender, System.EventArgs e)
{
Mcon.Open();
Dv = new DataView (Movset1.Tables[0]);
Dgv1.DataSource = Dv;

int CellCount = 0;
int Rowcount = 0;
foreach (DataGridViewRow dr in Dgv1.Rows)
{

foreach (DataGridViewCell dc in dr.Cells)
{
CellCount++;


}
Rowcount++;

}
MessageBox.Show(" there are " + CellCount + " cells ");


Yeah, That makes sense.
There are two statements inside the compound loop.
CellCount++; // inside loop
Rowcount++; // outside loop

I think both are required.set AutoGenerateColumns to true on the DataGridView.
AT the moment ive got Cell count and Row count and the data source of the datagridview(DGV1) is set as dataview. The table & rows show in Dataview.

I am unable to FIND why the data source is not showing in DGV1 which is why the foreach loops are not beng triggered. The reason am doin this is because I want to loop through the cells of the datagrid and see if there are any null cells. Am using the foreach loop at the moment to see if the cells can be picked up.

Ive tried using a Dataview but this doesnt seem to have a property to loop through the cells of the grid. Is is it really that hard to loop through a datagrid & check the values?
Quote from: BC_Programmer on June 12, 2013, 03:20:28 PM
set AutoGenerateColumns to true on the DataGridView.
I have managed to get it going by looping through the datatable & counting the blank cells. I loop through the datarows & then the datacells. The code for this is below. Thanks again for your help


Code: [Select] int CellCount = 0;
int Blk_Cell = 0;


DataTable MovTable = Movset1.Tables[0];
foreach (DataRow row in MovTable.Rows) // Loop over the rows.
{

foreach (var item in row.ItemArray) // Loop over the items.
{
CellCount++;

if (item.ToString() == "" && CellCount != 4)
{

MessageBox.Show(" This cell is blank ");
Blk_Cell++;

}
MessageBox.Show(item.ToString());
}
CellCount = 0;
}


MessageBox.Show(" there are " + Blk_Cell + " Blank Cells ");

So you never tried AutoGenerateColumns, I take it.

When I tested I found the DataGrid was empty from code, and a foreach iterator would skip- the same issue you were having. I changed the AutoGenerateColumns to true and the foreach iteration worked as intended.

I was using a BindingList rather than a Database-backed DataTable, though.I did try the AutoGeneratecolumns on the datagirdview but this still DIDNT work. But this is probably because I didnt use a binding list as you mentioned


Discussion

No Comment Found