combobox中的可选项如何能和数据库中我查询出来的表中的一列数据绑定?

解决方案 »

  1.   

    绑定啊! /// <summary>
    /// 添加ComboBox数据
    /// </summary>
    /// <param name="cb"></param>
    /// <param name="tableName"></param>
    /// <param name="textFieldName"></param>
    /// <param name="valueFieldName"></param>
    public static void AddComboBoxData(ComboBox cb,string storeProcedureName,string textFieldName,string valueFieldName,ConditionCollection cc)
    {
    DataSet ds = new DataSet();
    try
    { if (storeProcedureName.ToUpper().IndexOf(" FROM ") > 0)
    {
    DataAccess.Query(ds,storeProcedureName,CommandType.Text,cc,GlobalObject.GetInstance().Connection);
    }
    else
    {
    DataAccess.Query(ds,storeProcedureName,cc,GlobalObject.GetInstance().Connection);
    }
    if (ds.Tables.Count == 0)
    {
    throw new ArgumentNullException(storeProcedureName,"No table found after query.");
    }
    if (ds.Tables[0].Columns.Count == 0)
    {
    throw new ArgumentException(storeProcedureName,"No column found after query.");
    }
    if (ds.Tables[0].Columns.IndexOf(textFieldName) < 0)
    {
    throw new ArgumentException(textFieldName,"can not be found from the columns of datatable after query.");
    }
    if (ds.Tables[0].Columns.IndexOf(valueFieldName) < 0)
    {
    throw new ArgumentException(valueFieldName,"can not be found from the columns of datatable after query.");
    } cb.DisplayMember = textFieldName;
    cb.ValueMember = valueFieldName;
    cb.DataSource = ds.Tables[0];
    cb.DropDownStyle = ComboBoxStyle.DropDownList; }
    catch(Exception e)
    {
    Console.WriteLine(e.Message);
    }
    }
      

  2.   

    DataAccess是一个数据库封装,你换成自己的就可以!
      

  3.   

    JasonHeung,我刚学这个
    cb.DataSource = ds.Tables[0];这个是和一个表绑定啊,怎么和一个表中一列绑定呢?
      

  4.   

    JasonHeung,我刚学这个
    cb.DataSource = ds.Tables[0];这个是和一个表绑定啊,怎么和一个表中一列绑定呢?
    -------------------------------------------------------------------------------
    查询语句:
    ds中的数据从数据库中查询一列就可以。
    "select xxx from table"
    cb.DataSource = ds.Tables[0];
      

  5.   

    加上cb.DisplayMember=“xxx ”