Forum  General Visual ...  How do I...?  Combo Box in dataBound grid
Previous Previous
 
Next Next
New Post 8/2/2012 2:43 PM
  seeker61
5 posts
No Ranking


Combo Box in dataBound grid 

I want three of my columns in the datagrid to be comboboxes.  Following is the code that does not impliment the combos--

DataGridViewComboBoxColumn comboBoxSource = new DataGridViewComboBoxColumn();
            DataGridViewCheckBoxColumn chkBoxVisible = new DataGridViewCheckBoxColumn();
            DataGridViewComboBoxColumn comboboxFieldTitle = new DataGridViewComboBoxColumn();
            DataGridViewComboBoxColumn comboBoxType = new DataGridViewComboBoxColumn();
 

----------

 comboBoxSource.Items.Add("Database");
            comboBoxSource.Items.Add("Fixed");
            comboBoxSource.HeaderText = "Source";
            comboBoxSource.DataPropertyName = "FieldSource";
            //dgvFields.Columns[9].CellType.Equals(comboBoxSource);
            dgvFields.Columns[9].CellType.IsInstanceOfType(comboBoxSource);

--------

foreach(EP_Main.CustomReportField ForGrid in dsForGrid)
                {
 
                    comboboxFieldTitle.Items.Add(ForGrid.GetColumnValue("FieldTitle"));
                }

                    comboboxFieldTitle.HeaderText = "Field";
                    comboboxFieldTitle.DataPropertyName = "FieldTitle";
                    dgvFields.Columns[3].CellType.IsInstanceOfType(comboboxFieldTitle);

comboBoxType.Items.Add("String");
                    comboBoxType.Items.Add("Int32");
                    comboBoxType.Items.Add("Decimal");
                    comboBoxType.Items.Add("DateTime");
                    comboBoxType.Items.Add("Boolean");
                    comboBoxType.HeaderText = "Type";
                    comboBoxType.DataPropertyName = "FieldType";
                    dgvFields.Columns[6].CellType.IsInstanceOfType(comboBoxType);

 
New Post 8/8/2012 5:16 PM
  palli
14416 posts
1st Level Poster




Re: Combo Box in dataBound grid 

 Hi,

I am not exactly sure how to understand your code, but if I were to guess, then I would say that you must be misunderstanding the purpose of IsInstanceOfType and that you assume it will replace the column's cell type with the parameter type. This is not the case, as IsInstanceOfType is a boolean function that will return true or false depending on if the underlying datatype is the parameter type. 

You can see some examples on how to use the DataGridView control in our CompanionKit here.

Hope this helps,

Palli

 


Páll Björnsson - Visual WebGui support team - Email: support@visualwebgui.com
 
New Post 8/27/2012 4:56 PM
  bmsvic
36 posts
www.bms.com.au
No Ranking


Re: Combo Box in dataBound grid 
Modified By bmsvic  on 8/28/2012 1:10:49 AM)

 The example you refer to in the Companion kit only has a grid that is constructed in code.

There don't seem to be any examples of how to have a ComboBox Column for a databound grid.

Can you post some code on how to do this please?

 

 

 


Regards ..... MarkJ
Director Solution Architecture
www.bms.com.au
 
New Post 8/28/2012 3:16 PM
  palli
14416 posts
1st Level Poster




Re: Combo Box in dataBound grid 

 Hi,

The DataGridView has an AutoGenerateColumns property, which is by default set to True, and in that case it will automatically generate the DataGridView columns for you when you assign some datasource to DataGridView.DataSource.

This automatic generation of columns will not automatically generate DataGridViewComboBoxColumn columns for you, so you will have to do that manually (by code), or via the designer (which is also by code). 

Say to have a DataTable with two columns: Name, PostalCode and then you have another DataTable for Postal codes. You want to display the first table in your DataGridView, with PostalCode as ComboBox column.

You can approach this in two ways. You can leave the DataGridView.AutoGenerateColumns as True, assign the datasource and then remove the PostalCode column and add it back as a DataGridViewComboBoxColumn. Or, you can have DataGridView.AutoGenerateColumns as False, and build both your columns manually.

This is basically all shown in the CompanionKit's samples, but a simple code might look something like this:

 

        private void Form3_Load(object sender, EventArgs e)
        {
            DataTable main1 = new DataTable();
            main1.Columns.Add("Name");
            main1.Columns.Add("PostalCode");
            DataTable postal1 = new DataTable();
            postal1.Columns.Add("pPostalCode");
            postal1.Rows.Add("P1");
            postal1.Rows.Add("P2");
            PopulateAuto(this.dataGridView1, main1, postal1);
 
            DataTable main2 = new DataTable();
            main2.Columns.Add("Name");
            main2.Columns.Add("PostalCode");
            DataTable postal2 = new DataTable();
            postal2.Columns.Add("pPostalCode");
            postal2.Rows.Add("P1");
            postal2.Rows.Add("P2");
            PopulateManually(this.dataGridView2, main2, postal2);
        }
 
        private void PopulateAuto(DataGridView dgv, DataTable dtMain, DataTable dtPostal)
        {
            dgv.Columns.Clear();
            dgv.AutoGenerateColumns = true;
            dgv.DataSource = dtMain;
 
            dgv.Columns.Remove("PostalCode");
 
            DataGridViewComboBoxColumn cc = new DataGridViewComboBoxColumn();
            cc.DisplayMember = "pPostalCode";
            cc.ValueMember = "pPostalCode";
            cc.DataSource = dtPostal;
            cc.DataPropertyName = "PostalCode";
 
            dgv.Columns.Add(cc);
        }
 
        private void PopulateManually(DataGridView dgv, DataTable dtMain, DataTable dtPostal)
        {
            dgv.Columns.Clear();
            dgv.AutoGenerateColumns = false;
            dgv.DataSource = dtMain;
 
            DataGridViewTextBoxColumn tc = new DataGridViewTextBoxColumn();
            tc.DataPropertyName = "Name";
 
            DataGridViewComboBoxColumn cc = new DataGridViewComboBoxColumn();
            cc.DisplayMember = "pPostalCode";
            cc.ValueMember = "pPostalCode";
            cc.DataSource = dtPostal;
            cc.DataPropertyName = "PostalCode";
 
            dgv.Columns.Add(tc);
            dgv.Columns.Add(cc);
 
        }

 

Hope this helps,

Palli

 

 

 


Páll Björnsson - Visual WebGui support team - Email: support@visualwebgui.com
 
New Post 8/31/2012 5:19 PM
  bmsvic
36 posts
www.bms.com.au
No Ranking


Re: Combo Box in dataBound grid 

 Thank Palli,

That's brilliant bit of code - explains it very clearly.

Should be on the home page with flashing lights

 


Regards ..... MarkJ
Director Solution Architecture
www.bms.com.au
 
Previous Previous
 
Next Next
  Forum  General Visual ...  How do I...?  Combo Box in dataBound grid
.NET HTML5 Web, Cloud and Mobile application delivery | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2012 Visual WebGui®       Visual WebGui weblog on ASP.NET Gizmox Blog Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS