Link to home
Start Free TrialLog in
Avatar of csharp_learner
csharp_learnerFlag for Singapore

asked on

Making a column in DataGridView with ComboBox

Hi currently I have a DataGridView table read from a Oracle database,One of the column is meant to take in only "1" or "0" as a value, so I was wondering if I can make the whole column into a combobox and read the current value as well as letting th user to change using the combobox.

Thanks in advance for any help offered.
localSQL = "SELECT KEYID,TAG,DESC,STATE,FROM PRODUCT1";
                    OracleCommand cmdOracle = new OracleCommand(localSQL, localConnection);
                    cmdOracle.CommandType = CommandType.Text;
                    OracleDataReader drOracle = cmdOracle.ExecuteReader();
                    DataTable ordt = new DataTable();
                    OracleDataAdapter orda = new OracleDataAdapter();
                    orda.SelectCommand = cmdOracle;
                    orda.Fill(ordt);
                    dataView1.DataSource = ordt.DefaultView;
                    dataView1.RowsDefaultCellStyle.BackColor = Color.White;
                    dataView1.Columns[3].Width = 150;
                    dataView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightBlue;
                    drOracle.Close();

Open in new window

Avatar of DanSo1
DanSo1
Flag of Poland image

Hi
OK, try to do it that way:
right click on your datagridview, choose Edit columns...
in the list (left side) select your field
in properties for that field choose ColumnType and change it to "DataGridViewComboBoxColumn"
after that, you can choose property DisplayStyle : DropDownButton or ComboBox (there is a small difference - in combobox user can type, in DropDownButton user can only choose item).
Next things is to set properly those properties: DataSource, DisplayMember, ValueMember or/and Items.

I hope that it will help you.

Regards
   Danso
Avatar of csharp_learner

ASKER

Hi,Sorry for the late reply.
As my datagrid view is read from the database there is no items listed in the Edit Columns.
And when i tried to insert the items into the column the result is there will be 2 sets of datagridview side by side.
The left set is the one i assign to the datagridview using u're suggestion and hence there is a combobox,the 2nd set on the right is the current datagridview read from the database.
Everything what we doing in VS environment is also possible to do from source code. The easiest way to figure out how to do it is to see file xxxx.Designer.cs where your VS created code.
If you still have problem with that I will prepare some example code how to add combo in runtime.

Regards
  Danso
ASKER CERTIFIED SOLUTION
Avatar of DanSo1
DanSo1
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial