Help with iterating through cells in datagrid

Discussion in 'C#' started by markyjj, Jun 5, 2013.

  1. markyjj

    markyjj New Member

    Joined:
    Dec 7, 2005
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    I need help on a program I am currently creating as all I am trying to do is read all the cells in the datagrid to see if there are any null values and then display a message. I have racked my head around this for ages and I just cant find any code to help. Below is the code I am trying to use to search through each cell but this throws an exception that datatable cannot be converted to dataset. Any help would be appreciated.

    Code:
     string CellVal = null;
                int Rowcount = 0;
                CurrencyManager cm = (CurrencyManager)this.BindingContext[this.dataGrid1.DataSource];
                int rowCount = cm.Count;
                int colCount = ((DataTable)this.dataGrid1.DataSource).Columns.Count; 
                for (int row = 0; row < rowCount; row++)
                {
                    for (int col = 0; col < colCount; col++)
                    {
                         CellVal = this.dataGrid1[row, col].ToString();
                    }
                }
    
    
    
    
     
  2. Ami Desai

    Ami Desai Member

    Joined:
    Jan 5, 2017
    Messages:
    42
    Likes Received:
    17
    Trophy Points:
    8
    Location:
    Ahmedabad
    Home Page:
    http://www.ifourtechnolab.com/
    Hi,

    You can check this

    Code:
    private void button1_Click(object sender, System.EventArgs e)
    {
    CurrencyManager cm = (CurrencyManager)this.BindingContext[this.dataGrid1.DataSource];
    int rowCount = cm.Count;
    //assumes datasource is a datatable...
    int colCount = ((DataTable)this.dataGrid1.DataSource).Columns.Count;
    for(int row = 0; row < rowCount; row++)
    {
    for(int col = 0; col < colCount; col++)
    {
    object cellValue = this.dataGrid1[row, col];
    Console.Write(cellValue.ToString() + " ");
    }
    Console.WriteLine("");
    }
    }

    Thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice