Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Making a column in DataGridView with ComboBox

Posted on 2009-04-02
4
Medium Priority
?
393 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:csharp_learner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 7

Expert Comment

by:DanSo1
ID: 24076345
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
0
 

Author Comment

by:csharp_learner
ID: 24083473
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.
0
 
LVL 7

Expert Comment

by:DanSo1
ID: 24084688
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
0
 
LVL 7

Accepted Solution

by:
DanSo1 earned 1800 total points
ID: 24085050
Hi
Try this code. It's adding a column with combo where user can choose 0 or 1.

Regards
  Danso
            System.Windows.Forms.DataGridViewComboBoxColumn MyColumnGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
            this.dataGridView1.Columns.Add(MyColumnGridViewTextBoxColumn);
 
            MyColumnGridViewTextBoxColumn.DataPropertyName = "my_field_name";
            MyColumnGridViewTextBoxColumn.HeaderText = "This is header";
            MyColumnGridViewTextBoxColumn.Items.AddRange(new object[] {
            "0",
            "1"});
            MyColumnGridViewTextBoxColumn.Name = "MyColumnGridViewTextBoxColumn";
            MyColumnGridViewTextBoxColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            MyColumnGridViewTextBoxColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;

Open in new window

0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question