Announcement

Collapse
No announcement yet.

Frage zu DataGridViewComboBoxColumn

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Frage zu DataGridViewComboBoxColumn

    Hallo,

    ich verwende in einem DataGrid eine ComboBoxColumn und befülle das DataGrid und die ComboBox aus der Datenbank.
    Jetzt möchte ich das man zu den in der ComboBox vorhandenen Werten auch eigene hinzufügen kann.

    Bei einer normalen ComboBox geht das mit der Eigenschaft DropDownStyle. Geht das auch irgendwie mit einer DataGridViewComboBoxColumn.

    Vielen Dank im Voraus!

    tibazi

  • #2
    Hallo,

    selbstverständlich, man muss nur der Datenquelle (DataTable-Instanz im DataSet), an die DataGridViewComboBoxColumn gebunden wird, die zusätzlichen Datensätze hinzufügen. Wenn das DataSet mit der Ergebnismenge der SELECT-Abfrage gefüllt wurde, können nachträglich zusätzliche Datensätze der DataTable hinzugefügt werden, die dann nur im DataSet (aber nicht in der Datenbank) sichtbar sind.

    Comment


    • #3
      Hi Andreas,

      vielen Dank für deine Antwort. Das habe ich soweit verstanden. Mir ging es aber mehr um die grafische Benutzerschnittstelle.
      Eine normale ComboBox kann ich so einstellen das sie direkt benutzereingaben zulässt (der Cursor blinkt dann in der Combobox und man kann Eingaben vornehmen).

      Genau das gleiche möchte ich jetzt auch bei einer DataGridViewComboBoxColumn.

      Gruß

      Timo

      Comment


      • #4
        Google sei danke hab ichs jetzt hinbekommen:
        Code:
        G
        Private Sub dgv_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing
                'Allow user to enter new values for ALL DataGridViewComboBox controls in the DataGridView
                If (TypeOf e.Control Is DataGridViewComboBoxEditingControl) Then
                    Dim cmb As DataGridViewComboBoxEditingControl = CType(e.Control, DataGridViewComboBoxEditingControl)
                    If Not cmb Is Nothing Then
                        cmb.DropDownStyle = ComboBoxStyle.DropDown
                    End If
                End If
            End Sub
        
            Private Sub dgv_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgv.CellValidating
                'Allow user to enter new values for all DataGridViewComboBox controls in the DataGridView
                If (TypeOf CType(sender, DataGridView).EditingControl Is DataGridViewComboBoxEditingControl) Then
                    Dim cmb As DataGridViewComboBoxEditingControl = CType(CType(sender, DataGridView).EditingControl, DataGridViewComboBoxEditingControl)
                    If Not cmb Is Nothing Then
                        Dim grid As DataGridView = cmb.EditingControlDataGridView
                        Dim value As Object = cmb.Text
                        '// Add value to list if not there
                        If cmb.Items.IndexOf(value) = -1 Then
                            '// Must add to both the current combobox as well as the template, to avoid duplicate entries...
                            cmb.Items.Add(value)
                            Dim cmbCol As DataGridViewComboBoxColumn = CType(grid.Columns(grid.CurrentCell.ColumnIndex), DataGridViewComboBoxColumn)
                            If Not cmbCol Is Nothing Then
                                cmbCol.Items.Add(value)
                            End If
                        End If
                        grid.CurrentCell.Value = value
                    End If
                End If
            End Sub
        Hier der Link:
        http://forums.microsoft.com/MSDN/Sho...13855&SiteID=1

        Comment

        Working...
        X