-1

I am using VS 2008 and VB.NET as a language. I have two grids. In one grid, ii have user's data. e.g Mobile no, email and other information. In second grid, i have some already added alarm.

I need to attach two checkbox colums in each row in alarm gridview. On click of that checkbox, particular database operation will execute.

Currently, I added checkboxes into the datagridview but I can't check/uncheck them.

Please help me to resolve the check/uncheck problem and give me a hint to perform database operations of checkbox click.

This is how I added checkbox into the datagridview

Dim email As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn(True)
Dim sms As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn(True)

email.HeaderText = "Email"
email.ReadOnly = False
sms.HeaderText = "SMS"
sms.ReadOnly = False

UserAlarmDataGridView.Columns.Add(email)

UserAlarmDataGridView.Columns.Add(sms)

I tried to make the column's readonly property to false but it doesn't work.

Andrea
  • 11,801
  • 17
  • 65
  • 72
user2119449
  • 1
  • 1
  • 1

2 Answers2

0

Check the following code

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim AlarmColumn1 As New DataGridViewCheckBoxColumn(True)
    Dim AlarmColumn2 As New DataGridViewCheckBoxColumn(True)

    AlarmColumn1.Name = "Alarm1"
    AlarmColumn1.HeaderText = "Email"
    AlarmColumn1.ReadOnly = False

    AlarmColumn2.Name = "Alarm2"
    AlarmColumn2.HeaderText = "SMS"
    AlarmColumn2.ReadOnly = False

    UserAlarmDataGridView.Columns.Add(AlarmColumn1)
    UserAlarmDataGridView.Columns.Add(AlarmColumn2)
    UserAlarmDataGridView.ReadOnly = False
End Sub

to make database operations based on a CheckBox click, use the following

Private Sub UserAlarmDataGridView_CellContentClick(sender As Object, _
    e As System.Windows.Forms.DataGridViewCellEventArgs) Handles UserAlarmDataGridView.CellContentClick

    Dim ColumnName1 As String = UserAlarmDataGridView.Columns(e.ColumnIndex).Name

    If ColumnName1 = "Alarm1" Or ColumnName1 = "Alarm2" Then
        Dim CellCheckBox1 As DataGridViewCheckBoxCell = _
            CType(UserAlarmDataGridView.Rows(e.RowIndex).Cells(ColumnName1), DataGridViewCheckBoxCell)

        Dim CellCheckBoxState1 As String = CellCheckBox1.EditingCellFormattedValue.ToString

        MsgBox("Row = " & CellCheckBox1.RowIndex & vbCrLf & _
               "Column = " & CellCheckBox1.ColumnIndex & vbCrLf & _
               "State = " & CellCheckBoxState1)
    End If
End Sub
David -
  • 2,009
  • 1
  • 13
  • 9
0

A better way to capture the value of the checkbox state....

If CBool(DG1.Rows(e.RowIndex).Cells(clmChecked.Index).Value) Then
    'checked
Else
    'Not checked
End if
Erik Schroder
  • 63
  • 1
  • 6