14 Replies - 6836 Views - Last Post: 23 May 2011 - 01:36 PM Rate Topic: -----

#1 dougancil2010  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 12-July 10

Checkbox in datagrid not able to be checked.

Posted 17 May 2011 - 08:16 AM

I have the following code:

 Private Sub holidaysaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles holidaysaveButton.Click
        For Each dr As DataGridViewRow In DataGridView1.Rows
            Dim cell1 As String
            Dim cell2 As Date
            Dim cell3 As String
            Dim cell4 As String = "Holiday"

            If dr.IsNewRow Then Exit For

            If dr.Cells(4).Value = "True" Then
                Dim str1 As String = dr.Cells(1).Value.ToString
                Dim str2 As String = dr.Cells(2).Value.ToString
                Dim str3 As String = dr.Cells(3).Value.ToString
                Dim str4 As String = dr.Cells(4).Value.ToString
            End If
            If dr.Cells(4).Value IsNot Nothing Then
                cell1 = dr.Cells(1).Value.ToString()
                cell2 = dr.Cells(2).Value.ToString()
                cell3 = dr.Cells(3).Value.ToString()
                Dim con As New SqlConnection
                Dim cmd As New SqlCommand
                Try
                    con.ConnectionString = "Data Source=xxxxx;Initial Catalog=xxxxx;User ID=xxxxx;Password=xxxxx"
                    con.Open()
                    cmd.Connection = con
                    cmd.CommandText = "INSERT INTO exceptions (employeenumber, exceptiondate, minutes, code) VALUES ('" & cell1 & "','" & cell2 & "','" & cell3 & "','" & cell4 & "')"
                    cmd.ExecuteNonQuery()
                    MessageBox.Show("Your data has been updated")
                Catch ex As Exception
                    MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
                Finally
                    con.Close()
                End Try
            End If
            DailySummaryReview.holidayaddButton.Enabled = False
            Me.Close()
        Next
    End Sub



and when a user clicks on cell4, it's supposed to add the value of "Holiday" to my database and all of the data from that line. What's happening is that I can't even click on the checkbox. Can someone please assist.

Thank you

Doug

Is This A Good Question/Topic? 0
  • +

Replies To: Checkbox in datagrid not able to be checked.

#2 ranbir  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 27
  • Joined: 04-June 10

Re: Checkbox in datagrid not able to be checked.

Posted 18 May 2011 - 02:59 AM

If you do not get any error msg then
set the ReadOnly property of the column to false.
Was This Post Helpful? 0
  • +
  • -

#3 dougancil2010  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 12-July 10

Re: Checkbox in datagrid not able to be checked.

Posted 19 May 2011 - 07:53 AM

Ranbir,

The ReadOnly property of that column is set to False and I'm still not getting any errors.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Checkbox in datagrid not able to be checked.

Posted 19 May 2011 - 08:32 AM

So you have 4 columns or 5? remember column1 is actually column index 0.

Also, can you put these two line in to test something for me?

Right under your cell4 declaration put:

MsgBox(dr.Cells(3).ValueType.ToString)
MsgBox(dr.Cells(3).ReadOnly)

Run the code and reply back with what the 2 messageboxes show.
Was This Post Helpful? 1
  • +
  • -

#5 dougancil2010  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 12-July 10

Re: Checkbox in datagrid not able to be checked.

Posted 19 May 2011 - 12:21 PM

Charlie,

System.Decimal and True.

This post has been edited by dougancil2010: 19 May 2011 - 12:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Checkbox in datagrid not able to be checked.

Posted 19 May 2011 - 12:33 PM

Sorry, that should have been 4 instead of 3 for the cells index. Can you switch that and let me know
Was This Post Helpful? 0
  • +
  • -

#7 dougancil2010  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 12-July 10

Re: Checkbox in datagrid not able to be checked.

Posted 19 May 2011 - 12:57 PM

Charlie,

System.Boolean and True
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Checkbox in datagrid not able to be checked.

Posted 19 May 2011 - 01:46 PM

So it is readonly then. You need to find out where it's being set to readonly and fix that. That is why you can't check it. The True is where I have it checking that cell for what the readonly setting is. It's returning True meaning somewhere it is being set.

This post has been edited by CharlieMay: 19 May 2011 - 01:47 PM

Was This Post Helpful? 1
  • +
  • -

#9 dougancil2010  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 12-July 10

Re: Checkbox in datagrid not able to be checked.

Posted 19 May 2011 - 02:36 PM

That's odd because the readonly value in the setting itself for the checkbox is set to false.

Is there anything in my code that you can see that would set that to readonly?

here is the code as it stands now (this is for the entire form:)

Imports System.Data.SqlClient
Imports System.Data
Public Class AddHoliday
    Private Sub AddHoliday_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Scratchpad2TableAdapter.Fill(Me.MDRDataSet5.scratchpad2)
    End Sub
    Private Sub DataGridView1_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        Dim msg = String.Format("DataError occurred:\{0}\{1}\DataErrorContext: {2}", e.Exception.GetType().ToString(), e.Exception.Message, e.Context)
        MessageBox.Show(msg)
    End Sub
  Private Sub holidaysaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles holidaysaveButton.Click
        For Each dr As DataGridViewRow In DataGridView1.Rows
            Dim cell1 As String
            Dim cell2 As Date
            Dim cell3 As String
            Dim cell4 As String = "Holiday"

            For Each row As DataGridViewRow In DirectCast(Me.DataGridView1.Rows, IEnumerable)
                If Convert.ToBoolean(dr.Cells(4).Value) = "True" Then
                    Dim str1 As String = dr.Cells(1).Value.ToString
                    Dim str2 As String = dr.Cells(2).Value.ToString
                    Dim str3 As String = dr.Cells(3).Value.ToString
                    Dim str4 As String = dr.Cells(4).Value.ToString
                End If
                If dr.Cells(4).Value IsNot Nothing Then
                    cell1 = dr.Cells(1).Value.ToString()
                    cell2 = dr.Cells(2).Value.ToString()
                    cell3 = dr.Cells(3).Value.ToString()
                    Dim con As New SqlConnection
                    Dim cmd As New SqlCommand
                    Try
                        con.ConnectionString = "Data Source=xxxxx;Initial Catalog=xxxxx;User ID=xxxxx;Password=xxxxx"
                        con.Open()
                        cmd.Connection = con
                        cmd.CommandText = "INSERT INTO exceptions (employeenumber, exceptiondate, minutes, code) VALUES ('" & cell1 & "','" & cell2 & "','" & cell3 & "','" & cell4 & "')"
                        cmd.ExecuteNonQuery()
                        MessageBox.Show("Your data has been updated")
                    Catch ex As Exception
                        MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
                    Finally
                        con.Close()
                    End Try
                End If
                DailySummaryReview.holidayaddButton.Enabled = False
                Me.Close()
            Next
        Next

    End Sub
   
End Class



This post has been edited by dougancil2010: 19 May 2011 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Checkbox in datagrid not able to be checked.

Posted 20 May 2011 - 09:06 AM

how are you populating the datagridview? Is it by data entry or are you creating and filling a datasource.

This post has been edited by CharlieMay: 20 May 2011 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,462
  • Joined: 29-May 08

Re: Checkbox in datagrid not able to be checked.

Posted 20 May 2011 - 09:10 AM

What if the field in the datarow is marked as readonly?
Was This Post Helpful? 0
  • +
  • -

#12 dougancil2010  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 12-July 10

Re: Checkbox in datagrid not able to be checked.

Posted 20 May 2011 - 09:52 AM

Ok so I put a datagridview.readonly = false in my load event for the form and I am now able to click the checkbox, but it won't save the data if I click on any line other than the first line. I put a line break on the ExecuteNonQuery after the insert sql command, and when I click on lines 2 or 3, I'm never reaching that break point. I'm also not getting any errors. Any thoughts as to why it's not saving the data?

This post has been edited by dougancil2010: 20 May 2011 - 10:30 AM

Was This Post Helpful? 0
  • +
  • -

#13 dougancil2010  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 12-July 10

Re: Checkbox in datagrid not able to be checked.

Posted 23 May 2011 - 08:13 AM

Can someone please help me understand why I'm not reaching that break point?

Thank you
Was This Post Helpful? 0
  • +
  • -

#14 dougancil2010  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 12-July 10

Re: Checkbox in datagrid not able to be checked.

Posted 23 May 2011 - 01:31 PM

I have a form in an application that I'm building and in it I have a datagridview that when a user clicks a checkbox on a line in the datagridview and "submits" the data, then the form will insert all of the data on that line (along with the code "Holiday") into a database. Currently if I click in the checkbox on any line other than the first line and put a line break on the "Try" statement, and then fire the submit_onclick event, I'm never reaching that break point but if I click on the checkbox on the first line and submit that data, that works and it behaves at I stated above. Here is the code as it stands currently:
Private Sub holidaysaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles holidaysaveButton.Click

        For Each dr As DataGridViewRow In DataGridView1.Rows
            Dim cell1 As String
            Dim cell2 As Date
            Dim cell3 As String
            Dim cell4 As String = "Holiday"

            For Each row As DataGridViewRow In DirectCast(Me.DataGridView1.Rows, IEnumerable)
                If Convert.ToBoolean(dr.Cells(4).Value) = "True" Then
                    Dim str1 As String = row.Cells(1).Value.ToString
                    Dim str2 As String = row.Cells(2).Value.ToString
                    Dim str3 As String = row.Cells(3).Value.ToString
                    Dim str4 As String = row.Cells(4).Value.ToString
                End If
                If row.Cells(4).Value IsNot Nothing Then
                    cell1 = row.Cells(1).Value.ToString()
                    cell2 = row.Cells(2).Value.ToString()
                    cell3 = row.Cells(3).Value.ToString()
                    Dim con As New SqlConnection
                    Dim cmd As New SqlCommand
                    Try
                        con.ConnectionString = "Data Source=xxxxx;Initial Catalog=xxxxx;User ID=xxxxx;Password=xxxxx"
                        con.Open()
                        cmd.Connection = con
                        cmd.CommandText = "INSERT INTO exceptions (employeenumber, exceptiondate, minutes, code) VALUES ('" & cell1 & "','" & cell2 & "','" & cell3 & "','" & cell4 & "')"
                        cmd.ExecuteNonQuery()
                        MessageBox.Show("Your data has been updated")
                    Catch ex As Exception
                        MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
                    Finally
                        con.Close()
                    End Try
                End If
                DailySummaryReview.holidayaddButton.Enabled = False
                Me.Close()
            Next
        Next


Can someone please tell me what I may be missing?

Thank you

Doug
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9181
  • View blog
  • Posts: 34,470
  • Joined: 12-June 08

Re: Checkbox in datagrid not able to be checked.

Posted 23 May 2011 - 01:36 PM

Merging duplicate topics... please don't do that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1