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

关于ComboBox绑定DataSet的问题。
comboBox.DisplayMember = "aa"; //列名1
comboBox.ValueMember = "bb"; //列名2
comboBox.DataSource = dataSet.Tables[0].DefaultView;

为什么下拉列表里总显示“System.Data.DataRowView”?

------解决方案--------------------
comboBox.DataSource = dataSet.Tables[0]; 
comboBox.DisplayMember = "aa"; //列名1 
comboBox.ValueMember = "bb"; //列名2 

不要写DefaultView
------解决方案--------------------
正是! 不要把后面的defaultview加上去
这样就一定不会再出现那样的问题了
------解决方案--------------------
不行的话,用
for(int i=0;i<dataset.Tables[0].Rows.Count;i++)
{
combobox.items.add(dataset.Tables[0].Rows[i][0]);
}

------解决方案--------------------
好的 可以搞定 

DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
//----------初始dataGridView
string str = "Provider=Microsoft.Jet.OLEDB.4.0;data source=Northwind.mdb";
OleDbConnection con = new OleDbConnection(str);
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "select * from Customers";
OleDbDataAdapter adp = new OleDbDataAdapter();
adp.SelectCommand = cmd;


adp.Fill(ds, "Customers");
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
//this.dataGridView1.DataBind();


//-------初始combobox
this.comboBox1.DataSource = this.ds.Tables["Customers"].DefaultView;
this.comboBox1.DisplayMember = "CustomerID";

}

这样写可以 我试过了
------解决方案--------------------
好的 可以搞定 

DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
//----------初始dataGridView
string str = "Provider=Microsoft.Jet.OLEDB.4.0;data source=Northwind.mdb";
OleDbConnection con = new OleDbConnection(str);
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "select * from Customers";
OleDbDataAdapter adp = new OleDbDataAdapter();
adp.SelectCommand = cmd;


adp.Fill(ds, "Customers");
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
//this.dataGridView1.DataBind();


//-------初始combobox
this.comboBox1.DataSource = this.ds.Tables["Customers"].DefaultView;
this.comboBox1.DisplayMember = "CustomerID";

}

这样写可以 我试过了
------解决方案--------------------
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.comboBox1.Text);
}
------解决方案--------------------
加不加DefaultView没什么影响。
应该是你的数据集dataset数据填充有问题。。
你把这一段代码发出来看看。。

------解决方案--------------------
不加DefaultView就行
如果还有问题
查下数据源取到了没有
------解决方案--------------------
不是那里的问题 这个我见过 当时搞定了
是这里的问题 
 private void button1_Click(object sender, EventArgs e) 

MessageBox.Show(this.comboBox1.Text); 
}