简体   繁体   中英

Show values from datagridView in combobox

I have for example this values in datagridView : 1;2;3;4;

I want display this values in combobox like this :
1
2
3
4
My code show only last value in combobox: 4
My code of showing :

        string cmbValue = CmbText;
        string[] cmb = cmbValue.Split(new[] { ';' },StringSplitOptions.RemoveEmptyEntries);

        for (int i = 0; i < cmb.Length; i++)
        {
            comboBox1.Text = cmb[i];

        }

and here is my code of creating array of combobox and calling method to change cell in dataGrid:

            string cmbText = comboBox1.Text;
            string[] cmb = new string[] { cmbText};

            frm1.ChangeCellCmb(2, cmb);
            this.Dispose();

Someone know how to do this ? I cant simply set collection of values combobox, because values of combobox are reading from datagrid and it's reading from DB. Many thanks.

You need to loop and add the required items as follows:

for (int i = 0; i < cmb.Length; i++)
    comboBox1.Items.Add(cmb[i]);

This will add all the required items to the drop down menu. To select/display '4' the 3rd entry in cmb by default do

comboBox1.SelectedIndex = 3;

or

comboBox1.SelectedItem = "4";

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM