简体   繁体   中英

Selecting an item in ComboBox, which is Data bound

On my main form, there's dataGridView1 and it's bound to a database table. On my edit form, there's a ComboBox which gets it's items via Data Binding to the column from the same database as dataGridView1 . On dataGridView1 cell double click, that edit form opens and populates all the fields needed for editing that certain line.

Here's my problem: When I try to set the value of ComboBox on that edit form, nothing happens. No error is given and the item in ComboBox is not selected.

fDodaj nov = new fDodaj();
nov.comboBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString();
if(nov.ShowDialog()==DialogResult.OK)
{
//code that updates the value in database
}

fDodaj is my editing form. I've tryed setting SelectedValue , SelectedItem , SelectedText and Text properties on ComboBox , no luck in any case.

What am I doing wrong?

PS: I'm not very goot at Visual C# , but this is for my school project. If you need any more info, just let me know.

Move your ComboBox data binding into the constructor of your form, rather than keeping it in the Form.Load as you're currently doing. Ensure that it is bound after the call to InitializeComponent in your constructor.

This will allow you to set the ComboBox SelectedValue or similar before the dialog is shown.

Data binding occurs after form shown so when you are setting the selected value there is nothing in data source of combobox Set the data source of combobox before of setting selected value

I'm not good at Visual C# either. But did you try this inside the Form_Load of fDodaj. To me it looks like combo might not be initialized at this point.

First of all, it is not a good practice to make control public, so you can access to them over forms (or classes), like you did in your example: nov.comboBox1.Text Its always good to have them private, and pass values (data) to them. This is how you should do:

//form1
    public partial class Form1 : Form
    {
        DataTable table;
        public Form1()
        {
            InitializeComponent();
            table = new DataTable("myTable");
            table.Columns.Add("column 1", typeof(string));

            //some example data:
            table.Rows.Add("a");
            table.Rows.Add("b");
            table.Rows.Add("c");
            dataGridView1.DataSource = table;

            dataGridView1.CellDoubleClick += new DataGridViewCellEventHandler(dataGridView1_CellDoubleClick);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string _value = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
            if (_value != String.Empty)
            {
                using (Form2 f2 = new Form2(_value))
                {
                    if (f2.ShowDialog() == DialogResult.OK)
                    { 

                    }
                }
            }
        }
    }

//form2:
    public partial class Form2 : Form
    {
        public Form2(string value)
        {
            InitializeComponent();
            //some example data in the comboBox:
            comboBox1.Items.AddRange(new string[] { "a", "b", "c" });

            //lets select the item which came from form1:
            comboBox1.SelectedItem = value;
        }
    }

If this is not it, please let me know, will help you further. bye, bye

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