-1

我使用VS 2008和VB.NET作爲語言。我有兩個網格。在一個網格中,ii有用戶的數據。例如手機號碼,電子郵件和其他信息。在第二格,我有一些已經增加了警報。將複選框列添加到DataGridView中

我需要在報警gridview的每一行中附加兩個複選框列。點擊該複選框後,將執行特定的數據庫操作。

目前,我添加複選框到datagridview中,但我無法檢查/取消選中它們。

請幫我解決檢查/取消選中的問題,並給我一個提示,執行復選框單擊的數據庫操作。

我這是怎麼加入到複選框在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) 

我試圖讓列的只讀屬性設置爲false,但它不工作。

回答

0

檢查下面的代碼

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 

,使基於一個複選框點擊數據庫操作,使用下面的

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 
+0

Thanx。這是有幫助的...... :) – user2119449

0

一種更好的方式來捕捉複選框狀態值....

If CBool(DG1.Rows(e.RowIndex).Cells(clmChecked.Index).Value) Then 
    'checked 
Else 
    'Not checked 
End if