C# Foreach statement not working on datagrid
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: C# Foreach statement not working on datagrid

  1. #1
    Join Date
    Dec 2005
    Posts
    21

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

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,174

    Re: C# Foreach statement not working on datagrid

    What doesn't work?

  3. #3
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

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

  4. #4
    Join Date
    Dec 2005
    Posts
    21

    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.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,174

    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.

  6. #6
    Join Date
    Dec 2005
    Posts
    21

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center