自动选择组合框项目 [英] select combobox item automatically

查看:87
本文介绍了自动选择组合框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..我有一个datagridview,它显示带有公司名称和产品类型的各种产品.还有一个文本框和两个组合框,一个具有公司名称,另一个具有产品类型.当我单击dgv的特定行时,我想在组合框中选择公司名称和产品类型.我尝试将indexof()方法与正确的参数一起使用.但是它总是返回-1作为异常.但是我的组合框已经包含了该特定项目.

我帮我.

我的代码如下.

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.

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);
            }
        }

推荐答案

如果组合框绑定到数据源,则无法使用控件的text属性对其进行更改.如果直接更新数据网格,也将不起作用.
当您考虑它时,它变得非常合乎逻辑,因为您将更新屏幕值,但这不会反映数据源中的实际值.您必须使用数据源更改值才能实际更改网格中的值.因此,如果将组合框连接到用于网格的相同数据源,它将在选择时自动更新其他控件.这使他们了解数据"并对数据更改做出反应.通过使用数据源简单地连接它们,您就不必自己处理它了,当然也容易得多.

祝你好运!
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!


这篇关于自动选择组合框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆