简体   繁体   中英

Leaving a Combo Box cell in a datagridview

I have a datagridview with three columns. The first two are combo boxes; the third is a checkbox. If I choose a value from the drop down for the combo box and then immediate exit the datagridview by clicking on a button outside the grid, the value I selected is not remember. If I look at that cell it contains db.null. If I click on some other cell before exiting the grid that the cell value is retained.

I cannot expect my users to remember to do this. What is the way around this issue? Also what is the event that gets triggered after I select a value from my combo box drop down.

I have been asked in the comments to post the relevant code, so here it is. Thanks to alybaba726 for responding.

Code to build the grid dynamically

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"

code to bind the grid to the table

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

code to read the grid data

  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

I got the answer in another forum and I wanted to share it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM