comboBox设置了下拉菜单后,如何将当前内容赋予string类型,我用 string a=comboBox.text不行,a为空值

解决方案 »

  1.   

    不会把,你看看comboBox.text有没有值
      

  2.   

    string a=comboBox.SelectedItem.ToString();
      

  3.   

    string sql = "select * from zy_bhjfyz" + Convert.ToString(starttime.Year) + starttime.Month.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + " and " + days(Convert.ToDateTime(date2)) + " and yzmc like '%" + f.comboBox1.Text + "%'";
      

  4.   

    在下拉菜单选取内容运行后发现 comboBox为空值
      

  5.   

    comboBox里的值是手动在Items里加的还是数据库里读取赋值的呢?
      

  6.   

    这个如果绑定,而且选择了值并看到了cmb中有值,不可能出现.text无值的吧。
    除非选择的时候触发了什么事件。导致.text值没有了。
      

  7.   

    是读取的数据库的值
    SqlConnection conn = new SqlConnection(str);
                string Sql = "select mc  from zd_yp order by mc";
                DataSet Ds = new DataSet();
                SqlDataAdapter Da = new SqlDataAdapter(Sql, conn);
                Da.Fill(Ds, "zd_ry");
                comboBox1.DataSource = Ds.Tables["zd_ry"];
                comboBox1.DisplayMember = "mc";
      

  8.   

    我只有一个Dropdown事件啊,读取数据库的,其他的都没有
      

  9.   

    肯定是你在取comboBox.text之前,触发了什么操作。调试一下,看运行的时候comboBox.text是否有值
      

  10.   

    还是获取不了值啊 public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string str = "Data Source=192.168.1.1;Database=cbyy;Uid=his;Pwd=samanagger";
                SqlConnection conn = new SqlConnection(str);
                string Sql = "select mc  from zd_yp order by mc";
                DataSet Ds = new DataSet();
                SqlDataAdapter Da = new SqlDataAdapter(Sql, conn);
                Da.Fill(Ds, "zd_ry");
                comboBox1.DataSource = Ds.Tables["zd_ry"];
                comboBox1.DisplayMember = "mc";
                comboBox1.ValueMember = "mc";
                comboBox1.SelectedValue= "mc"; 
            }这是触发器的代码
     string sql = "select * from zy_bhjfyz" + Convert.ToString(starttime.Year) + starttime.Month.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + " and " + days(Convert.ToDateTime(date2)) + " and yzmc like '%" + f.comboBox1.Text+"%'";这是SQL语句的代码
      

  11.   

    comboBox1_SelectedIndexChanged事件中绑定comboBox1,这个做法不晓得有什么意图。
    不要在这个事件中绑定,在界面加载的时候绑定。
      

  12.   

    我就一个界面,读取数据用的,改为了      private void comboBox1_MouseClick(object sender, MouseEventArgs e)
            {
                string str = "Data Source=192.168.1.1;Database=cbyy;Uid=his;Pwd=samanagger";
                SqlConnection conn = new SqlConnection(str);
                string Sql = "select mc  from zd_yp order by mc";
                DataSet Ds = new DataSet();
                SqlDataAdapter Da = new SqlDataAdapter(Sql, conn);
                Da.Fill(Ds, "zd_ry");
                comboBox1.DataSource = Ds.Tables["zd_ry"];
                comboBox1.DisplayMember = "mc";
                comboBox1.ValueMember = "mc";
                comboBox1.SelectedValue= "mc"; 
            }点击事件还是一样的
      

  13.   

    找到问题了,我的SQL语句写在另外一个类在,获取不了FORM1 这个类的值
      

  14.   

     Form1 f = new Form1();
                    string sql = "select * from zy_bhjfyz" + Convert.ToString(starttime.Year) + starttime.Month.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + " and " + days(Convert.ToDateTime(date2)) + " and yzmc ='" + f.comboBox1.Text+"'";
    为什么  f.comboBox1.Text 的值为空?