C# Foreach statement not working on datagrid

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 831 Views - Last Post: 16 June 2013 - 08:21 AM Rate Topic: -----

#1 markyjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-August 09

C# Foreach statement not working on datagrid

Posted 11 June 2013 - 12:01 PM

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 ");





Is This A Good Question/Topic? 0
  • +

Replies To: C# Foreach statement not working on datagrid

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8371
  • View blog
  • Posts: 31,100
  • Joined: 12-June 08

Re: C# Foreach statement not working on datagrid

Posted 11 June 2013 - 12:05 PM

03	            Dgv1.DataSource = this.Dv.Table;

Perhaps tell it which table in the collection of tables is the one you want to set the datasource to.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2877
  • View blog
  • Posts: 9,548
  • Joined: 12-December 12

Re: C# Foreach statement not working on datagrid

Posted 11 June 2013 - 01:58 PM

    Rowcount++;

You increment Rowcount, but display CellCount, which is still 0.
Was This Post Helpful? 0
  • +
  • -

#4 markyjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-August 09

Re: C# Foreach statement not working on datagrid

Posted 14 June 2013 - 11:37 AM

I assigned the dataview to the datagridview and I am unable to find why the data source is not showing in DGV1(Datagridview). 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?
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2877
  • View blog
  • Posts: 9,548
  • Joined: 12-December 12

Re: C# Foreach statement not working on datagrid

Posted 14 June 2013 - 11:44 AM

So did you perform the correction in my previous post, and try modi123_1's suggestion?

Post your current code. Do you receive errors? If so, post these as well, together with confirmation as to which lines they refer to in your posted code.
Was This Post Helpful? 0
  • +
  • -

#6 optix212  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 446
  • Joined: 10-October 09

Re: C# Foreach statement not working on datagrid

Posted 14 June 2013 - 12:18 PM

Your program is not bypassing anything. It is definitely looping through the rows, but you are incrementing the wrong thing like Modi said. You declared CellCount like you should, outside of the foreach statement.. But you are declaring another variable called RowCount, and incrementing it by 1 every time it loops. You should rewrite it like this:
 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 ");


This post has been edited by optix212: 14 June 2013 - 12:18 PM

Was This Post Helpful? 1
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2877
  • View blog
  • Posts: 9,548
  • Joined: 12-December 12

Re: C# Foreach statement not working on datagrid

Posted 14 June 2013 - 04:46 PM

+1 despite the mis-attribution ;)
Was This Post Helpful? 0
  • +
  • -

#8 markyjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-August 09

Re: C# Foreach statement not working on datagrid

Posted 15 June 2013 - 05:57 AM

View Postoptix212, on 14 June 2013 - 01:18 PM, said:

Your program is not bypassing anything. It is definitely looping through the rows, but you are incrementing the wrong thing like Modi said. You declared CellCount like you should, outside of the foreach statement.. But you are declaring another variable called RowCount, and incrementing it by 1 every time it loops. You should rewrite it like this:
 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 ");


Was This Post Helpful? 0
  • +
  • -

#9 markyjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-August 09

Re: C# Foreach statement not working on datagrid

Posted 15 June 2013 - 06:03 AM

I have tried the previous code with the CellCount++ but this doesnt seem to work as the loop is not incrementing and the code just skips to the cell count & displays 0 cells.

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. IN the end I want to be able to recognise if the cell is null.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2877
  • View blog
  • Posts: 9,548
  • Joined: 12-December 12

Re: C# Foreach statement not working on datagrid

Posted 15 June 2013 - 06:25 AM

If nothing is showing in the DGV then it has no rows to loop through. So the problem is with assigning the DataSource correctly, not the loop.

You need to examine Movset1.Tables[0] and find out why it is not populating, or why this is not subsequently populating the DGV.

This post has been edited by andrewsw: 15 June 2013 - 06:41 AM

Was This Post Helpful? 0
  • +
  • -

#11 markyjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-August 09

Re: C# Foreach statement not working on datagrid

Posted 15 June 2013 - 08:06 AM

Do I need to bind the datasource to the datagridview as well
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2877
  • View blog
  • Posts: 9,548
  • Joined: 12-December 12

Re: C# Foreach statement not working on datagrid

Posted 15 June 2013 - 08:28 AM

View Postmarkyjj, on 15 June 2013 - 03:06 PM, said:

Do I need to bind the datasource to the datagridview as well

You might read this MSDN link - bind a DataView to a DataGridView.

A guess would be that this line:

Dgv1.DataSource = this.Dv.Table;

perhaps should be:

Dgv1.DataSource = this.Dv;

but I haven't studied this in any detail. You might also add the following line just after:

Dgv1.AutoResizeColumns();

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2877
  • View blog
  • Posts: 9,548
  • Joined: 12-December 12

Re: C# Foreach statement not working on datagrid

Posted 15 June 2013 - 08:34 AM

Any warnings in your code - blue wiggly lines?
Was This Post Helpful? 0
  • +
  • -

#14 markyjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-August 09

Re: C# Foreach statement not working on datagrid

Posted 15 June 2013 - 04:24 PM

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 ");



Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3162
  • View blog
  • Posts: 9,547
  • Joined: 05-May 12

Re: C# Foreach statement not working on datagrid

Posted 15 June 2013 - 07:37 PM

I'll ask the stupid question: what is so special about the 4th column? Why do you check for CellCount != 4 on line 13?

This post has been edited by Skydiver: 15 June 2013 - 07:37 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2