Home > Software engineering >  Get ComboBox ValueMember from DataGridview
Get ComboBox ValueMember from DataGridview

Time:07-27

I'm trying to get the ValueMember of a ComboBox that is in a Datagridview cell from a button click event:

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet ds = new DataSet();

            OleDbDataAdapter t3DataAdapter = new OleDbDataAdapter(new OleDbCommand("SELECT * FROM Adok;", conn));
            OleDbCommandBuilder t3Command = new OleDbCommandBuilder(t3DataAdapter);
            t3DataAdapter.FillSchema(ds, SchemaType.Source, "Adok");

            for (int i = 0; i < dataGridView3.Rows.Count; i  )
            {
                DataRow row4 = ds.Tables["Adok"].NewRow();
                row4["HoltID"] = ((ComboBox)dataGridView3["HoltID", i]).ValueMember ;// I would like to get something like this

                ds.Tables["Adok"].Rows.Add(row4);
            }
        }
   }

CodePudding user response:

The problem was that I created a ComboBox in CellBeginEdit event, but in the CellEndEdit event I hid it and the value was added to the cell value. (So the cell type was a DataGridViewTextBoxCell at the end, not a DataGridViewComboBoxCell)

So I used the the Tag property of the cell in CellEndEdit to save also the ID.

Now, everything works.

  • Related