comboBox_SelectedIndexChanged不起作用 [英] comboBox_SelectedIndexChanged Not working

查看:70
本文介绍了comboBox_SelectedIndexChanged不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,

我有一个comoboBox,我试图用SQL中的值填充comboBoz。

我一直在努力尝试,什么都没有,我得到了连接和值显示但我不能更改为其他值。

Guys,
I have a comoboBox and im trying to fill the comboBoz with values from the SQL.
I've been trying and trying and nothing and i get connected and values is shown but i cant change to the other values.

private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
              
         {
             try
             {
                 con.Open();
                 SqlDataAdapter comd = new SqlDataAdapter("Select * From Stocks ", con);
                 System.Data.DataTable dt = new System.Data.DataTable();
                 //rdr = cmd.ExecuteReader();
                comboBox1.DataSource = dt;
                comd.Fill(dt);
                 con.Close();
                 comboBox1.DisplayMember = "Product_Name";
                 //  textBox2.Text = rdr.GetString(1);
                 //  textBox3.Text = rdr.GetString(2);
                 
                 
                

             }
             catch (Exception i)
             {
                 
                 MessageBox.Show(i.Message);

             }
         }





我从这段代码得到的是正确的一切都显示在comboBox

但是我选择了其他第二个值的值

任何想法为什么会这么讨厌

推荐答案

嗯,你的逻辑有点缺陷。

你正在改变SelectedIndexChanged事件中组合框的数据源。

这意味着您更改了所选索引的含义。



此代码应在FormLoad中执行,或者当您按下Initial按钮或类似按钮时执行。

Hmm, your logic is a bit flawed.
You are changing the data source of the combobox inside the SelectedIndexChanged event.
This means that you change the meaning of the selected index.

This code should be executed in FormLoad or when you press an Initial button or similar.
con.Open();
SqlDataAdapter comd = new SqlDataAdapter("Select * From Stocks ", con);
System.Data.DataTable dt = new System.Data.DataTable();
//rdr = cmd.ExecuteReader();
comboBox1.DataSource = dt;
comd.Fill(dt);
con.Close();
comboBox1.DisplayMember = "Product_Name";





在SelectedIndexChanged事件中,您应该对所选产品名称对您的应用程序的意义采取行动。





Inside the SelectedIndexChanged event you should act upon what the selected Product Name means for your application.

private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
    DataRow dr = (comboBox1.SelectedItem as DataRow);
    if (dr != null)
    {
        // This is just an example, I have no idea what you want to do.
        string product = dr["Product_Name"].ToString();       
    }
}





你还应该考虑不使用查询



You should also consider not using the query

SELECT * FROM table



效率不高,最好指定你真正需要的列。



还可以考虑设置 comboBox1.ValueMember 如果它与 comboBox1.DisplayMember 不同。

ValueMember可能是主键表,例如。


It is not very efficient and it is better to specify the columns you really need.

Also consider setting comboBox1.ValueMember if it is different from comboBox1.DisplayMember.
ValueMember could be the Primary key of the table, for example.


这个怎么样



What about this one

private void Form1_Load(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            try
            {
                con.Open();
                SqlDataAdapter comd = new SqlDataAdapter("Select * From Stocks ", con);
                System.Data.DataTable dt = new System.Data.DataTable();
                con.Close();
                //rdr = cmd.ExecuteReader();
                comd.Fill(dt);
             //   comboBox1.DisplayMember = "Product_Name";

                comboBox1.DataSource = dt;


            }
            catch (Exception i)
            {
                //con.Close();
                MessageBox.Show(i.Message);

            }
            finally
            {

            }
       
        }







private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)

        {
            try
            {
               comboBox1.DisplayMember = "Product_Name";
                }
            catch (Exception i)
            {

                MessageBox.Show(i.Message);


1。据我说。首先创建一个这样的方法。



私有void Display_stocks()

{

con.Open() ;

SqlDataAdapter dr = new SqlDataAdapter(Select * From Stocks,con);

System.Data.DataSet ds = new System.Data.DataSet();

dr.Fill(ds,Stock); //在DataSet中填充值



comboBox1.DataSource = ds.Tables [0]; //将数据集转换为DataTable。

con.Close();

comboBox1.DisplayMember =Product_Name;

}



然后这个方法调用form_Load。



2.SelectedIndexChanged事件把这段代码。



private void comboBox1_SelectedIndexChanged_1(object sender,EventArgs e)

{

string product = comboBox1.Text;



}
1. According to me . First create one method like this.

Private void Display_stocks()
{
con.Open();
SqlDataAdapter dr= new SqlDataAdapter("Select * From Stocks ", con);
System.Data.DataSet ds = new System.Data.DataSet();
dr.Fill(ds,"Stock"); // Fill value in DataSet

comboBox1.DataSource = ds.Tables[0]; //Convert Dataset into DataTable.
con.Close();
comboBox1.DisplayMember = "Product_Name";
}

Then this method call in form_Load .

2.SelectedIndexChanged event put this code.

private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
string product=comboBox1.Text;

}


这篇关于comboBox_SelectedIndexChanged不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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