-
June 11th, 2013, 01:58 PM
#1
C# Foreach statement not working on datagrid
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. I need help please. Below is the code with the loop.
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 ");
-
June 11th, 2013, 09:00 PM
#2
Re: C# Foreach statement not working on datagrid
-
June 12th, 2013, 08:45 AM
#3
Re: C# Foreach statement not working on datagrid
The count of cells you have defined is called CellCount. This is the variable you are trying to display. However, in your foreach, you are incrementing the variable RowCount. Also, in the messagebox.show you are not converting CellCount to a string. Try this.
Code:
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.ToString() + " Cells ");
-
June 12th, 2013, 02:03 PM
#4
Re: C# Foreach statement not working on datagrid
Thanks for the reply. I have tried the code above but the 1st foreach statement is not counting through the rows and doesnt even reach the 2nd Foreach loop. I have checked the code in the debugger and the table & rows is showing in Dv but not in Dgv1(datagridview) when Dv is assigned as the datasource to Dgv1.
-
June 12th, 2013, 05:49 PM
#5
Re: C# Foreach statement not working on datagrid
There is no real need to loop through the dataview since you have access to the data source directly. In other words, just go to the source of the data directly.
-
June 15th, 2013, 06:19 PM
#6
Re: C# Foreach statement not working on datagrid
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
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 ");
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|