954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problems filling a checkbox

Hello,
I am having a problem.
I am filling a datagridview straight from an oracle sql query.
The datagridview is filling properly using this code.

Try
            cmd.CommandText = "SELECT *" & _
           "FROM oracle database 

            Dim da As New OracleDataAdapter(cmd)
            Dim DMdt As New DataTable
            da.Fill(DMdt)

            If DMdt.Rows.Count > 0 Then

                Me.DataGridView1.DataSource = DMdt
              'filter the datatable
                da.Dispose()


I then add a column to the datagridview

Dim cbPart As New DataGridViewCheckBoxColumn
                DataGridView1.Columns.Insert(1, cbPart)
                With cbPart
                    .HeaderText = "Part"
                    .Name = "Part"
                    .DisplayIndex = 10
                    .Frozen = False
                End With


The column shows up just fine. However here is when I call a parsing sub to check each box when certain part formats are met. The parsing is working fine in debugging but when a value is true it is throwing an exception.

'do all parsing 
'if conditions are met then
 CType(DataGridView1.DataSource, DataTable).Rows(i).Item("Part") = True
                count = count + 1

The error I am getting is stating that the Part column I am creating does not belong to the table. However it is showing up in the datagridview.
Any help would be appreciated.
Thanks,
Starlight849

starlight849
Light Poster
29 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Your code would work perfectly if you manually populated the DataGridView.
But because the grid is databound, any changes to the grid layout, ie a new column, will result in an error because that column is not part of the original database table.
The error occurs because the change in the datagrid will trigger an event in the grid to perform a change in the underlying datatable.

Now, if you add a boolean column to the datatable before binding it to the datagrid, then the checkbox column will be shown, and any changes to it will not result in error.

Try
  cmd.CommandText = "SELECT *" & _
  "FROM oracle database 

  Dim da As New OracleDataAdapter(cmd)
  Dim DMdt As New DataTable
  da.Fill(DMdt)

  If DMdt.Rows.Count > 0 Then
    DMDt.Columns.Add(New DataColumn("Part", GetType(Boolean)))

    Me.DataGridView1.DataSource = DMdt
    'filter the datatable
    da.Dispose()
Oxiegen
Master Poster
715 posts since Jun 2006
Reputation Points: 87
Solved Threads: 141
 

You're amazing! Works perfectly! Thanks so much!

starlight849
Light Poster
29 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: