0

I have a datagridview on a Winforms app where I add a DataGridViewComboBoxColumn. The datagridview is not visible (on a different tab) when the program starts and tries to set the DgvComboBox's value. It appears in my case that the DataGridView combobox's value cannot be set until I go to the tab that contains the Dgv, and gets painted. Then I can go back to the original tab and set the combobox all day long.

    dgvRequirements.DataSource = ReqDB.GetNewRequirements()

    Dim col As New System.Windows.Forms.DataGridViewComboBoxColumn
    col.DropDownWidth = 400
    col.DataPropertyName = "Responsible"
    col.Name = "Responsible"
    col.HeaderText = "Responsible"
    col.Items.Add(" ")


    col.DataSource = activeEmployees
    col.DisplayMember = "Name"
    col.ValueMember = "empId"

    col.DropDownWidth = 400


    dgvRequirements.Columns.Insert(2, col)

The issue is I am trying to set the value of the column programmatically.

dgvRequirements.Rows(index).Cells("Responsible").Value = cboDefault.SelectedValue

The DatagridView is on a different tab. No matter how many times I attempt to set the ComboBox column's value, it will not set, until FIRST I go to the tab where the datagridview is on. Once the Dgv gets painted, and I go back to set the value, then the value can be set without any problem.

Joseph
  • 93
  • 1
  • 11
  • 1
    The TabControl does interfere with that. Use the TabControls Selecting event to set the value of the controls within that TabPage. – LarsTech Mar 17 '19 at 03:55
  • 1
    To elaborate on what @LarsTech said, the `TabControl` is designed to be efficient by not creating controls on `TabPages` until that `TabPage` is selected. That is how it can prevent a GUI with large numbers of controls being slow to load. It also means that you cannot access controls on other than the first `TabPage` when the form loads, so you need to allow for that and access them later, when their parent `TabPage` is selected for the first time. If that's not possible, you need to force creation of those controls on form load. – jmcilhinney Mar 17 '19 at 04:03
  • Related question: [Do you have to show every tab before all textboxes actually populate?](https://stackoverflow.com/questions/50108553/do-you-have-to-show-every-tab-before-all-textboxes-actually-populate/50119854#50119854). Set `TabPageWithDGV.Visible = True' before trying to manipulate the DGV. – TnTinMn Mar 17 '19 at 13:21

0 Answers0