日期:2014-05-17  浏览次数:20870 次

COMBOX绑定问题。
C# code
 
        private void BindCategory()
        {
            string sql = ReportForm.Properties.Settings.Default.CEC710ConnectionString;
            SqlConnection conn = new SqlConnection(sql);

            DataSet1 myData = new DataSet1();
            SqlDataAdapter da = null;
           
            da = new SqlDataAdapter("select b.name from BuildNumber a inner join Category b on a.BNumber = b.BNumber where a.Description = '" + comboBox1.SelectedValue + "'", conn);
            da.Fill(myData, "Category");

            this.comboBox3.DataSource = myData;
            this.comboBox3.DisplayMember = myData.Tables[0].Columns[2].ColumnName;
            this.comboBox3.ValueMember = myData.Tables[0].Columns[2].ColumnName;
        }

  this.comboBox3.DataSource = myData;
  this.comboBox3.DisplayMember = myData.Tables[0].Columns[2].ColumnName;
  this.comboBox3.ValueMember = myData.Tables[0].Columns[2].ColumnName;这些地方有问题,该怎么绑定呢?我主要是要显示Category的第三列里边的值。

------解决方案--------------------
this.comboBox3.DisplayMember = myData.Tables[0].Columns[0].ColumnName;
this.comboBox3.ValueMember = myData.Tables[0].Columns[0].ColumnName;

------解决方案--------------------
this.comboBox3.DataSource = myData.Tables["Category"];
this.comboBox3.DisplayMember = "name";
this.comboBox3.ValueMember = "name";

------解决方案--------------------

C# code
            this.comboBox3.DataSource = myDataTables[0];
            this.comboBox3.DisplayMember = myData.Tables[0].Columns[2].ColumnName;
            this.comboBox3.ValueMember = myData.Tables[0].Columns[2].ColumnName;

------解决方案--------------------
private void BindCategory()
{
string sql = ReportForm.Properties.Settings.Default.CEC710ConnectionString;
SqlConnection conn = new SqlConnection(sql);

DataSet1 myData = new DataSet1();
SqlDataAdapter da = null;

da = new SqlDataAdapter("select b.name as CategoryName from BuildNumber a inner join Category b on a.BNumber = b.BNumber where a.Description = '" + comboBox1.SelectedValue + "'", conn);
da.Fill(myData, "Category");

this.comboBox3.DataSource = myData;
this.comboBox3.DisplayMember = myData.Tables[0].Columns[2].ColumnName;
this.comboBox3.ValueMember = myData.Tables[0].Columns[2].ColumnName;
}
this.comboBox3.DataSource = myData.Tables[0];
this.comboBox3.DisplayMember = "CategoryName";
this.comboBox3.ValueMember = "CategoryName";