Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Datagridview & Combobox

Empfohlene Antworten

Veröffentlicht

Hallo Zusammen,

In meiner Anwendung verwende ich ein DataGridView (ohne DB!) und in einer Spalte soll man Werte aus einer Combobox auswählen können.

Das Problematische: Die Combobox soll keine Einschränkung sein, der User soll komplett neue Einträge direkt in die Zelle schreiben können. Und die Combobox soll sich dann (falls der direkt eingegebene Wert neu ist, um diesen Wert erweitern).

Ich bin auch schon ganz gut unterwegs... Hier mein Code:


        private void Form1_Load(object sender, EventArgs e)

        {

            m_grid_view.Columns.Add("Spalte1", "Spalte1");


                DataGridViewComboBoxColumn test = new DataGridViewComboBoxColumn();

                test.Items.Add("abc");

                test.Items.Add("def");

                m_grid_view.Columns.Add(test);

                m_grid_view.EditingControlShowing +=

                        new DataGridViewEditingControlShowingEventHandler(

                        dataGridView1_EditingControlShowing);


                m_grid_view.CellValidating +=

                           new DataGridViewCellValidatingEventHandler

                               (dataGridView1_CellValidating);


            }

            private void dataGridView1_EditingControlShowing(object sender,

        DataGridViewEditingControlShowingEventArgs e)

            {

                if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))

                {

                    DataGridViewComboBoxEditingControl cbo = e.Control as DataGridViewComboBoxEditingControl;

                    cbo.DropDownStyle = ComboBoxStyle.DropDown;

                    //cbo.TextChanged += new EventHandler(cbo_TextChanged);

                    //cbo.Leave += new EventHandler(cbo_Leave);

                }


            }

            private void dataGridView1_CellValidating

                (object sender, DataGridViewCellValidatingEventArgs e)

            {

                CellValidate(e.FormattedValue.ToString());


            }


            private void CellValidate(string value)

            {

                DataGridViewComboBoxCell cell =

                    m_grid_view.CurrentCell as DataGridViewComboBoxCell;


                if (cell != null &&

                    !cell.Items.Contains(value))

                {


                    // Insert the new value into position 0

                    // in the item collection of the cell

                    cell.Items.Insert(0, value);

                    // When setting the Value of the cell, the  

                    // string is not shown until it has been

                    // comitted. The code below will make sure 

                    // it is committed directly.

                    if (m_grid_view.IsCurrentCellDirty)

                    {

                        // Ensure the inserted value will 

                        // be shown directly.

                        // First tell the DataGridView to commit 

                        // itself using the Commit context...

                        m_grid_view.CommitEdit

                            (DataGridViewDataErrorContexts.Commit);

                    }

                    // ...then set the Value that needs 

                    // to be committed in order to be displayed directly.

                    cell.Value = cell.Items[0];

                }

                else

                {

                    if (cell != null)

                        cell.Value = cell.Items[cell.Items.IndexOf(value)];

                }



            }

Das Problem ist, im DataGridView ist die letzte Zeile ja immer für "Neue Zeilen" da... Hier kann der User was eintragen, und das DataGridView verlängert sich automatisch um eine Zeile.

Wenn der User aber jetzt in der "neuen Zeile" in der Spalte direkt einen Wert eintippt, dann wird dataGridView1_CellValidating nicht aufgerufen :(

Der User muss also zuerst einen Wert in der Combobox auswählen, damit die Zeile "bestätigt" wird, und erst dann funktioniert das CellValidating ereigniss...

Habt ihr eine Idee wie ich mein Problem lösen kann?

Viele Grüße

Stefan

Hallo Zusammen,

In meiner Anwendung verwende ich ein DataGridView (ohne DB!) und in einer Spalte soll man Werte aus einer Combobox auswählen können.

Das Problematische: Die Combobox soll keine Einschränkung sein, der User soll komplett neue Einträge direkt in die Zelle schreiben können. Und die Combobox soll sich dann (falls der direkt eingegebene Wert neu ist, um diesen Wert erweitern).

Du hast ne GridView in der 2 Spalten sind... in einer kann man was eintragen in der andren was durch die combobox auswählen.... falls er was neues reinschreibt soll sich die combobox aktualisieren um den Wert der eingetragen wordn ist.... falls schon vorhanden soll er das in der combobox selektieren...

ich frag mich wo der sinn davon ist mir fällt jetzt spontan nix ein wo man sowas usen könnte wenn in der nächsten spalte schon die combobox ist...

versuchmal die ereignisse übern designer zu regris... und verwend das ereignis CellValidated.

und verwend mal


m_grid_view.CellValidated += new DataGridViewCellEventHandler(this.deine_Methode);

und übergib deiner Methode kein
DataGridViewCellValidatingEventArgs e 

Objekt, sondern versuchs mal mit
DataGridViewCellEventArgs e 

sollte normal dein Problem lösen

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.