3 Replies - 5320 Views - Last Post: 05 June 2013 - 11:40 AM

#1 markyjj  Icon User is offline

  • New D.I.C Head

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

Help with iterating through datagrid cells

Posted 04 June 2013 - 04:29 PM

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.

 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();
                }
            }



Is This A Good Question/Topic? 0
  • +

Replies To: Help with iterating through datagrid cells

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3822
  • View blog
  • Posts: 13,544
  • Joined: 12-December 12

Re: Help with iterating through datagrid cells

Posted 04 June 2013 - 05:06 PM

What line does the error occur on?
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3822
  • View blog
  • Posts: 13,544
  • Joined: 12-December 12

Re: Help with iterating through datagrid cells

Posted 04 June 2013 - 05:16 PM

I found the following code suggesting that you may have to cast the DataSource to a DataView.

        ByVal e As EventArgs)
        Dim dv1 = DirectCast(DataGridView1.DataSource, DataView)
        Dim dv2 As New DataView(dv1.Table)
        Dim cma = DirectCast(BindingContext(dv1), CurrencyManager)
        dv2.RowFilter = "Name = '" & dv1(cma.Position)("Name").ToString & "'"
        DataGridView2.DataSource = dv2

but it is hard to tell if this is necessary with your code. Worth a shot though. Edited: This is in VB.NET but the principles are the same for C#.

Do you have Option Explicit On? Recommended. Not relevant, sorry.

This post has been edited by andrewsw: 05 June 2013 - 12:37 PM

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: Help with iterating through datagrid cells

Posted 05 June 2013 - 11:40 AM

The error occurs on line 5. The code compiles ok but the exception is thrown when the part of code is executed during the program.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1