none
DataGridViewComboBoxColumn selected value problem RRS feed

  • Question

  • Hello,

    I have DataGridView containing DataGridViewComboBoxColumn. I generate a dataset1 and i fill this column combobox  with the table from the dataset1. Now the DataGridView is filled with the table from another dataset, dataset2, it contains a column that should be the selected value in the DataGridViewCombobox column. How do I set this? How do i set the selected value of the combobox column is from the dataTable that DataGridView is taking the data from?

    Thanks

     

    Friday, October 10, 2008 7:07 AM

Answers

  • Thanks Kira but I found better soluton.

     

    The DataGridViewComboBoxColumn has DataSource containing 2 colums, one is set as the displayMember and the other is  the ValueMember. Also DataPropertyName has to be set to the column from the dataset used to fill the DataGridView.

     

    The important thing is that DisplayMember, ValueMember and DataPropertyName are of datatype String. So in the SQL Query for the DataGridView, the column name in DataPropertyName has to be String. 

     

    Also here is a link that helped me a lot:  http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx

    Monday, October 27, 2008 7:06 AM

All replies

  • Hi Barbi

     

    I have written an example for you to implement this.

    Code Snippet

        public partial class Form1 : Form

        {

            private DataSet ds1 = new DataSet();

            private DataSet ds2 = new DataSet();

            private DataTable dtSource = new DataTable("dtSource");

            private DataTable dtComboBox = new DataTable("dtComboBox");

            private ComboBox cmb;

     

            public Form1()

            {

                InitializeComponent();

     

                dtComboBox.Columns.Add("Value");

                dtComboBox.Columns.Add("Name");

                dtComboBox.Rows.Add(1, "Item1");

                dtComboBox.Rows.Add(2, "Item2");

                dtComboBox.Rows.Add(3, "Item3");

                dtComboBox.Rows.Add(4, "Item4");

                ds1.Tables.Add(dtComboBox);

     

               

                dtSource.Columns.Add("Column1");

                dtSource.Columns.Add("Column2");

                dtSource.Rows.Add(1, "test1");

                dtSource.Rows.Add(2, "test2");

                dtSource.Rows.Add(3, "test3");

                dtSource.Rows.Add(4, "test4");

                ds2.Tables.Add(dtSource);

               

     

                DataGridViewComboBoxColumn dgvComboBox = new DataGridViewComboBoxColumn();

                dgvComboBox.DisplayMember = "Name";

                dgvComboBox.HeaderText = "Column1_Name";

                dgvComboBox.ValueMember = "Value";

                dgvComboBox.DataPropertyName = "Column1";

                dgvComboBox.DataSource = ds1.Tables["dtComboBox"];

                dataGridView1.Columns.Add(dgvComboBox);

     

                dataGridView1.DataSource = ds2;

                dataGridView1.DataMember = "dtSource";

     

                DataGridViewTextBoxColumn dgvText = new DataGridViewTextBoxColumn();

                dgvText.HeaderText = "Column1_Value";

                dataGridView1.Columns.Insert(0, dgvText);

     

                dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);

                dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);

            }

     

            void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

            {

                if (e.RowIndex < dtSource.Rows.Count)

                {

                    dataGridView1[0, e.RowIndex].Value = dtSource.Rows[e.RowIndex][0];

                }

            }

     

            void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

            {

                cmb = e.Control as ComboBox;

                if (cmb != null)

                {

                    cmb.SelectedValueChanged += new EventHandler(cmb_SelectedValueChanged);

                }

            }

     

            void cmb_SelectedValueChanged(object sender, EventArgs e)

            {           

                this.dataGridView1[0, dataGridView1.CurrentRow.Index].Value = cmb.SelectedValue;

            }

    }

     

     

    The CellFormatting event of the DataGridView allow you to fill the Column1_Value at the first run. Then you can handle the EditingControlShowing event of the DataGridView to set the first column(Column_Value) cell value according to the ComboBox select action.

     

    Sincerely,

    Kira Qian

     

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

    Monday, October 13, 2008 9:39 AM
  • Adding event handlers just ot initialize selected item? This is too much. In asp.net (even in 2003) is much much easier, with 2 rows of code this can be acheaved. It can't be the only solution.

    Friday, October 24, 2008 6:24 AM
  • My solution is handle the EditingControlShowing event of DataGridView and SelectedValueChanged event of ComboBox. A lot of code is just for test. The key point is the bold font. If you want to simplify it, then the CellFormatting event is not needed. The solution of Winform may be different with ASP.NET.

     

    Sincerely,

    Kira Qian

    Friday, October 24, 2008 6:47 AM
  • Thanks Kira but I found better soluton.

     

    The DataGridViewComboBoxColumn has DataSource containing 2 colums, one is set as the displayMember and the other is  the ValueMember. Also DataPropertyName has to be set to the column from the dataset used to fill the DataGridView.

     

    The important thing is that DisplayMember, ValueMember and DataPropertyName are of datatype String. So in the SQL Query for the DataGridView, the column name in DataPropertyName has to be String. 

     

    Also here is a link that helped me a lot:  http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx

    Monday, October 27, 2008 7:06 AM
  • Thank you for sharing your experience Barbi, it will be useful to the person who view this thread.

    Monday, October 27, 2008 7:11 AM