2015-06-08 63 views
0

我有一個包含三列的datagridview。前兩個是組合框;第三個是複選框。如果我從組合框的下拉列表中選擇一個值,然後通過單擊網格外的按鈕立即退出datagridview,那麼我選擇的值不會記住。如果我查看該單元格,它包含db.null。如果我在退出網格之前單擊其他單元格以保留單元格值。在datagridview中留下組合框單元格

我不能指望我的用戶記得這樣做。解決這個問題的方法是什麼?此外,從我的組合框下拉列表中選擇一個值後觸發的事件是什麼。

我已經在評論中被要求發佈相關代碼,所以在這裏。感謝alybaba726的迴應。

代碼來構建網格動態

dgvSchedule.AutoGenerateColumns = False 
    Dim hoursCol, timeOfDayCol As New DataGridViewComboBoxColumn 
    Dim deleteCol As New DataGridViewCheckBoxColumn 

For i As Integer = 1 To 12 
    hoursCol.Items.Add(i) 
Next 
timeOfDayCol.Items.Add("AM") 
timeOfDayCol.Items.Add("PM") 

hoursCol.DataPropertyName = "Hour" 
timeOfDayCol.DataPropertyName = "AM/PM" 
deleteCol.DataPropertyName = "Delete" 

dgvSchedule.Columns.Add(hoursCol) 
dgvSchedule.Columns.Add(timeOfDayCol) 
dgvSchedule.Columns.Add(deleteCol) 
dgvSchedule.Columns(0).HeaderText = "Hour" 
dgvSchedule.Columns(1).HeaderText = "AM/PM" 
dgvSchedule.Columns(2).HeaderText = "Delete" 

代碼到電網結合表

mDataTable = GetTable() 


    dgvSchedule.DataSource = mDataTable 

Function GetTable() As DataTable 
' Create new DataTable instance. 
Dim table As New DataTable 

' Create four typed columns in the DataTable. 
table.Columns.Add("Hour", GetType(Integer)) 
table.Columns.Add("AM/PM", GetType(String)) 
table.Columns.Add("Delete", GetType(Boolean)) 

For i = 1 To mSchedules.Count 
    table.Rows.Add(SetAMPMHour(mSchedules(i - 1).ScheduleINetHour), SetAMPM(mSchedules(i - 1).ScheduleINetHour), False) 

Next 

Return table 
End Function 

代碼讀取網格數據

Dim _Hour As String 
      Dim _AMPM As String 
      Dim _DeleteIt As Int16 

      For Each row As DataGridViewRow In dgvSchedule.Rows 

       _Hour = clsUtilities.NullIsBlank(row.Cells(0).Value) 
       _AMPM = clsUtilities.NullIsBlank(row.Cells(1).Value) 
       If _Hour <> "" Or _AMPM <> "" Then 
        If _Hour = "" Or _AMPM = "" Then 
         MsgBox("Incomplete Row Bypassed " & _Hour & " " & _AMPM) 
        Else 
         _DeleteIt = clsUtilities.NullIsZero(row.Cells(2).Value) 
         If UpdateCaseMixSchedule(GetAbsoluteHour(_Hour, _AMPM), _DeleteIt) = False Then 
          Return False 

         End If 
        End If 
       End If 

      Next row 
+0

你的datagridview綁定到數據源嗎? – alybaba726

+0

它綁定到我動態創建的表格。 –

+0

你可以發佈你目前使用的代碼嗎? – alybaba726

回答

0

我在另一個論壇得到了答案,我想分享它。

Private Sub dgvSchedule_CurrentCellDirtyStateChanged(sender As System.Object, e As System.EventArgs) Handles dgvSchedule.CurrentCellDirtyStateChanged 
     If dgvSchedule.IsCurrentCellDirty Then 
      dgvSchedule.CommitEdit(DataGridViewDataErrorContexts.Commit) 
     End If 

    End Sub 
相關問題