DataGridView Winforms Populate ComboBoxCell

I am creating one software in which a user will create an invoice using a datagridview. I have some cells in a text box and some cells with a list. There are some things I want to do:

  • I want to populate combobox cells from a database table. I tried this:

    DataTable tblItems = UtilityClass.GetDataTable("SELECT ItemName,ItemID FROM Items"); DataGridViewComboBoxColumn itemCol=DataGridViewComboBoxColumn)dataGridViewNewBill.Columns["ColItem"]; itemCol.DataSource = tblItems; itemCol.DisplayMember = "ItemName"; itemCol.ValueMember = "ItemID";

    But after selecting an element and when the focus moves to another cell, the element selected in the combo box (or text of the text field) the cell disappears.
    EDIT : it seems that when you select an item in the drop-down list, it forces all combobox cells to this value, since they have the same data source! But even if this is the case why a combox / textbox cell loses value after the focus has moved to another cell.

+3
source share
2 answers

Well, now I’m quite sure that when we use the same data source for two or more comboboxes, changing the value for one forces the others to change as well.

What I did was turn off the addition of new lines and

  • In the load form, one row is added, setting the data source of this particular cell with a list (not a column).
  • When the user goes to the last cell and press the enter key, I create another row and do the same as above.

So each cell has a different data source, although I need to do some more work, but it worked.

+1
source

Do you work with any specific events that erase data.

, Grid ComboBox combobox, EditingControl .

, EditingControlShowing

dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
+1

All Articles