Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   C# Foreach statement not working on datagrid (http://www.go4expert.com/forums/c-sharp-foreach-statement-datagrid-t29686/)

markyjj 12Jun2013 00:35

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. Below is the code with the loop. I need help please

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)
            MessageBox.Show(" there are " + CellCount + " Cells ");

All times are GMT +5.5. The time now is 09:10.