Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all.. I have a datagridview which displays various products with its company name and the type of product. There is also a textbox and two comboboxes, one having company name and the other having type of product. I want to select the company name and type of the product in the combobox when i click on the particular row of the dgv. I tried of using the indexof() method with the correct parameters. But it always returns -1 as exception. But my combobox already contains that particular item.

I help me.

My code is as follows.

C#
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                search = false;
                errorProvider1.Clear();
                msg_lbl.Text = "";
                msg_img.Image = InfoMate_Plus.Properties.Resources.empty_vista;
                if (dataGridView1.Rows.Count > 0)
                {
                    Brand_ID = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["ID"].Value);
                    Brand_textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["Brand"].Value.ToString();
                    string typ = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["Type"].Value.ToString();
                    string mak = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["Make"].Value.ToString();
                    SelectMake(mak);
                    SelectType(typ);
                    isedit = true;
                    current = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Index;
                    PositionItem.Text = " " + (current + 1).ToString();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Exception occured..", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void SelectMake(string mak)
        {
            try
            {
                msg_lbl.Text = Make_comboBox.Items.Count.ToString();
                int sel = Make_comboBox.Items.IndexOf(mak);
                Make_comboBox.SelectedItem = Make_comboBox.Items[sel];
            }

            catch (Exception ex)
            {
                MessageBox.Show("Exception occured.." + ex, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }
Posted

1 solution

If your combobox is bound to a data source you cannot change it using the text property of the control. It also wouldn't work if you update the data grid directly.
When you think about it, it becomes very logical because you would update the screen value, but this would not reflect the actual value in the datasource. You would have to change the value using the datasource to actually change the value in the grid. So, if you connect the combobox to the same datasource you use for your grid, it would automatically update the other control on selection. This makes them "data aware" and react on data changes. By simply connect them using the datasource you are making it unnecessary to handle it yourself and also a lot easier of course.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900