CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Jan 2015
    Posts
    4

    Question C# DataGridView ComboBoxColumn show Selected Item from the list.

    Hi,
    I am a novice developer using Visual Studio 2013 Express (C# and Win Forms Desktop) and trying to build a table in DataGridView. The table should contain simple columns and a few ComboBoxColumns. I get nice tables but the only thing that does not work is to show a selected item in the ComboBoxColumns although the information is in the dropdown lists.
    I have tried a lot of suggestions that are available on the web but until now nothing has worked. Several which have worked very well for others do not work for me. Anyone who could help? Looks like it's a general issue with no obvious simple solution.

    Grateful for any good advice,
    Mickejane.
    ____________________________________
    My model code which gives a simple table:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Test_ComboBoxColumn_2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.dataGridView1.DataSource = null;
                this.dataGridView1.Columns.Clear();
    
                dataGridView1.ColumnCount = 3;
                dataGridView1.Columns[0].Name = "Product ID";
                dataGridView1.Columns[1].Name = "Product Name";
                dataGridView1.Columns[2].Name = "Product Price";
    
    
                string[] row = new string[] { "1", "Product 1", "1000" };
                dataGridView1.Rows.Add(row);
                row = new string[] { "2", "Product 2", "2000" };
                dataGridView1.Rows.Add(row);
                row = new string[] { "3", "Product 3", "3000" };
                dataGridView1.Rows.Add(row);
                row = new string[] { "4", "Product 4", "4000" };
                dataGridView1.Rows.Add(row);
    
                DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
                cmb.HeaderText = "Select Data";
                cmb.Name = "cmb";
                cmb.MaxDropDownItems = 4;
                cmb.Items.Add("True");
                cmb.Items.Add("False");
               
                dataGridView1.Columns.Add(cmb);
               
                //Have tried a lot of suggestions here, e.g. 
                cmb.ValueMember = "True";
                // and many more complicated codes. Without success.
    
            }
        }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# DataGridView ComboBoxColumn show Selected Item from the list.

    Try adding the columns before adding your rows.

  3. #3
    Join Date
    Jan 2015
    Posts
    4

    Re: C# DataGridView ComboBoxColumn show Selected Item from the list.

    Quote Originally Posted by Arjay View Post
    Try adding the columns before adding your rows.
    I knew that a smart guy can fix it! It works! Thanks a lot! My long searches are over!

  4. #4
    Join Date
    Jan 2015
    Posts
    4

    Re: C# DataGridView ComboBoxColumn show Selected Item from the list.

    While I am very happy that there is now a solution to my problem, it is still a workaround of the actual issue. Adding rows after the columns I am entering an additional "New String" to the "surface space" of the combobox cell, which may be identical or not to any of the items on the combobox dropdown list:
    string[] row = new string[] { "1", "Product 1", "1000", "New String" };
    dataGridView1.Rows.Add(row);

    The real issue is how to bring an existing string item FROM the dropdown list of combobox cell to its "surface space". Outside of DataGridView it is done by ComboBox.SelectedIndex = int;.
    I haven't found a viable method how to do the same within the boundaries of DataGridView panel.
    Cheers.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# DataGridView ComboBoxColumn show Selected Item from the list.

    Not sure I follow. Can you zip up your test solution and post it here and explain where your program doesn't do what you would like it to do?

  6. #6
    Join Date
    Jan 2019
    Posts
    1

    Re: C# DataGridView ComboBoxColumn show Selected Item from the list.

    Quote Originally Posted by Arjay View Post
    Not sure I follow. Can you zip up your test solution and post it here and explain where your program doesn't do what you would like it to do?
    Original source: Add Combobox to DataGridView

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# DataGridView ComboBoxColumn show Selected Item from the list.

    It's been nearly 4 years... If you need a hand, start a new post.

  8. #8
    Join Date
    Jan 2015
    Posts
    4

    Re: C# DataGridView ComboBoxColumn show Selected Item from the list.

    Obviously I just have to accept that comboboxes outside and inside DataGridView are handled differently. Since I have a solution now how to do what I need also inside DataGridView (by adding rows after columns), it doesn't matter that the procedures are different.
    Thanks again for helping me!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured