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)
                {
                    Rowcount++;


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

You could also use this approach, to walk through each cell of the datagridview:

foreach (DataGridViewRow row in Dgv1.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {      
                    // your code here
                    MessageBox.Show(cell.Value.ToString());
                }
            }

Edited 3 Years Ago by ddanbe: addition

I have tried the code in the previous reply but it doesnt seem to work, as the datasource is not showing in datagridview(Dgv1)

I am unable to find why the data source is not showing in DGV1. Ive even tried setting the data source as the datatable but still nothing is showing in DGV1. 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?

There is no reason why you cannot use two for loops and Dgv1[x,y].Value;
If data is not showing in the datagridview, does your datsource contain any?

Thats what I cant understand because the dataview shows the datasource ok in the debugger but when I assign the dataview as the datasource to the dategridview, then nothing is showing in the datagridview. The thing is the dataview doesnt have a property that loops through the cells which is why I am trying to use the datagridview to do this.

Assign the data to a datatable. Use that as the datasource. Loop through the rows and cells of the datatable.

Edited 3 Years Ago by tinstaafl

I managed to get it going by your advice guys thanks alot. Below is the code I am using by looping the datatable & using loops through the rows & cells.

            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 ");
This question has already been answered. Start a new discussion instead.