从表中获取组合框中的项目时出错

时间:2013-12-08 20:01:35

标签: c# events

运行以下代码,我发现SelectedIndexChanged方法在加载方法之前运行...如何更正此问题?我尝试重置事件但不起作用

namespace adotestquestion 
{
    public partial class Bill : Form
    {
        public Bill()
        {
            InitializeComponent();
        }
        string constring = "data source=NISHANT-PC ; initial catalog=NIK_DATABASE ; user id=xxxxxx ; password=xxxxxx";
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter adapter;
        DataSet ds;
        DataTable dt;
        int qty, tax, total, price;

        private void Bill_Load(object sender, EventArgs e)
        {
            con = new SqlConnection(constring);
            cmd = new SqlCommand("select billid from bill", con);
            adapter = new SqlDataAdapter(cmd);
            ds = new DataSet();
            adapter.Fill(ds);
            dt = ds.Tables[0];
            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "billid";
            comboBox1.ValueMember = "billid";
            MessageBox.Show(id.ToString());
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(comboBox1.Text);
            int id;
            id = Convert.ToInt32(comboBox1.Text);
            con = new SqlConnection(constring);
            cmd = new SqlCommand("select * from bill where billid=@id", con);
            // cmd.Parameters.Add("@id", Convert.ToInt32(comboBox1.Text));
            cmd.Parameters.Add("@id", id);
            adapter = new SqlDataAdapter(cmd);
            ds = new DataSet();
            adapter.Fill(ds);
            dt = ds.Tables[0];

            foreach (DataRow dr in dt.Rows)
            {
                textBox1.Text = dr[1].ToString();
                textBox2.Text = dr[2].ToString();
                textBox3.Text = dr[4].ToString();
                textBox4.Text = dr[3].ToString();
                textBox5.Text = dr[5].ToString();
                textBox6.Text = dr[6].ToString();
                textBox7.Text = dr[7].ToString();
                textBox8.Text = dr[8].ToString();
                textBox9.Text = dr[9].ToString();
                textBox10.Text = dr[10].ToString();
            }
        }

    }
}

4 个答案:

答案 0 :(得分:1)

使用InitializeComponent方法取消订阅活动,并在Form_Load的末尾汇总。

答案 1 :(得分:1)

您可以简单地检查事件中组合框中是否确实选择了任何内容,而不是尝试更改事件等。这将使代码保持简单并且能够正常工作。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex >= 0)
    {
        // your existing code goes here
    }
}

答案 2 :(得分:1)

第三种方法是创建一个私有的类级变量,如isFormLoading,最初将其设置为true,然后在false结束时将其设置为Bill_Load事件。

您可以在comboBox1_SelectedIndexChanged以及其他任何需要的位置检查变量的值,以确定代码块是否应该运行。

但实际上,任何其他提供的答案都可行。

答案 3 :(得分:1)

问题:无论何时将项目绑定到ComboboxSelectedIndexChanged事件都会被触发。

解决方案:SelectedIndexChanged事件中,您需要识别Load事件或由于项目选择更改而触发的事件。

您可以声明boolean变量,并在控件进入true事件时将其设置为Load

来自selectedIndexChanged事件的

仅在boolean变量为false时执行代码。

注意:在Load事件结束时,再次将boolean变量更改为false,以便SelectionChanged事件在ComboBox实际选择时触发 bool loadevent = false; private void Bill_Load(object sender, EventArgs e) { loadevent = true; con = new SqlConnection(constring); cmd = new SqlCommand("select billid from bill", con); adapter = new SqlDataAdapter(cmd); ds = new DataSet(); adapter.Fill(ds); dt = ds.Tables[0]; comboBox1.DataSource = dt; comboBox1.DisplayMember = "billid"; comboBox1.ValueMember = "billid"; MessageBox.Show(id.ToString()); loadevent = false; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (!loadevent) { MessageBox.Show(comboBox1.Text); int id; id = Convert.ToInt32(comboBox1.Text); con = new SqlConnection(constring); cmd = new SqlCommand("select * from bill where billid=@id", con); // cmd.Parameters.Add("@id", Convert.ToInt32(comboBox1.Text)); cmd.Parameters.Add("@id", id); adapter = new SqlDataAdapter(cmd); ds = new DataSet(); adapter.Fill(ds); dt = ds.Tables[0]; foreach (DataRow dr in dt.Rows) { textBox1.Text = dr[1].ToString(); textBox2.Text = dr[2].ToString(); textBox3.Text = dr[4].ToString(); textBox4.Text = dr[3].ToString(); textBox5.Text = dr[5].ToString(); textBox6.Text = dr[6].ToString(); textBox7.Text = dr[7].ToString(); textBox8.Text = dr[8].ToString(); textBox9.Text = dr[9].ToString(); textBox10.Text = dr[10].ToString(); } } } 事件1}}。

试试这个:

{{1}}
相关问题