Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: C# Foreach statement not working on datagrid  (Read 16315 times)

0 Members and 1 Guest are viewing this topic.

markyjj

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Unknown
    C# Foreach statement not working on datagrid
    « on: June 11, 2013, 01:08:31 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

    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]

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: C# Foreach statement not working on datagrid
    « Reply #1 on: June 11, 2013, 02:30:56 PM »
    What is  Rowcount ?
    What gives it a start value?

    markyjj

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Unknown
      Re: C# Foreach statement not working on datagrid
      « Reply #2 on: June 11, 2013, 04:45:06 PM »
      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 bypassed

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: C# Foreach statement not working on datagrid
      « Reply #3 on: June 11, 2013, 04:54:45 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?
      I was trying to dereference Null Pointers before it was cool.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: C# Foreach statement not working on datagrid
      « Reply #4 on: June 11, 2013, 07:29:47 PM »
      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.

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: C# Foreach statement not working on datagrid
      « Reply #5 on: June 11, 2013, 08:31:50 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?
      I was trying to dereference Null Pointers before it was cool.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: C# Foreach statement not working on datagrid
      « Reply #6 on: June 11, 2013, 09:50:35 PM »
      What are you on about?
      I am wrong. Sorry.

      markyjj

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Unknown
        Re: C# Foreach statement not working on datagrid
        « Reply #7 on: June 12, 2013, 12:49:41 PM »
        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 ");



        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: C# Foreach statement not working on datagrid
        « Reply #8 on: June 12, 2013, 02:46:39 PM »
        Yeah, That makes sense.
        There are two statements inside the compound loop.
        CellCount++;        // inside loop
        Rowcount++;        // outside loop

        I think both are required.

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: C# Foreach statement not working on datagrid
        « Reply #9 on: June 12, 2013, 03:20:28 PM »
        set AutoGenerateColumns to true on the DataGridView.
        I was trying to dereference Null Pointers before it was cool.

        markyjj

          Topic Starter


          Greenhorn

          • Experience: Beginner
          • OS: Unknown
          Re: C# Foreach statement not working on datagrid
          « Reply #10 on: June 14, 2013, 12:43:41 PM »
          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?

          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 1140
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 11
          Re: C# Foreach statement not working on datagrid
          « Reply #11 on: June 14, 2013, 01:17:12 PM »
          set AutoGenerateColumns to true on the DataGridView.
          I was trying to dereference Null Pointers before it was cool.

          markyjj

            Topic Starter


            Greenhorn

            • Experience: Beginner
            • OS: Unknown
            Re: C# Foreach statement not working on datagrid
            « Reply #12 on: June 15, 2013, 05:29:05 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     


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


            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: C# Foreach statement not working on datagrid
            « Reply #13 on: June 15, 2013, 06:17:25 PM »
            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 was trying to dereference Null Pointers before it was cool.

            markyjj

              Topic Starter


              Greenhorn

              • Experience: Beginner
              • OS: Unknown
              Re: C# Foreach statement not working on datagrid
              « Reply #14 on: June 16, 2013, 09:24:36 AM »
              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