有dataset, 怎样绑定到combobox?

roxette909 2005-07-08 01:32:20
一个函数返回了一个dataset,怎样绑定到combobox?
...全文
1854 10 打赏 收藏 举报
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
julong88 2005-07-10
随便找本书都有吧
  • 打赏
  • 举报
回复
berlin8600 2005-07-10
如果你添充DataSet的类中是这样写的
myAdapter.Fill(myDataSet)
那么
Dim myDataSet as New DataSet
myDataSet = myDB.GetDataSet(strSql)
ComboBox1.DataSource = myDataSet

ComboBox1.DisplayMember = "UserName"
ComboBox1.ValueMember = "UserID"

就行了,不用加表名
  • 打赏
  • 举报
回复
ekin7913046 2005-07-10
ComboBox1.DataSouce = DataSet.Table["表名"];
ComboBox1.ValueMember = "值字段" ;
ComboBox1.DisplayMember = "显示的字段";
  • 打赏
  • 举报
回复
marvelstack 2005-07-09
ComboBox1.DataSource = myDataSet ;//数据集
ComboBox1.DisplayMember = "person.userId" ;//表名加字段名
ComboBox1.ValueMember = "person.username" ;

this.ComboBox1.DataSource = myDataTable;//数据表
this.ComboBox1.DisplayMember = "Name";字段名
this.ComboBox1.ValueMember = "userId";字段名
  • 打赏
  • 举报
回复
luyesql 2005-07-08
楼上的高手都说了.
  • 打赏
  • 举报
回复
lovefootball 2005-07-08
comboBox.DataSource=一个datatable;
comboBox.DisplayMember=这是你想显示的字段;
comboBox.ValueMember=显示字段对应的值,一般为主键;

比如表中有字段id,name

comboBox.DataSource = dt;//dt是select表的结果的datatable
comboBox.DisplayMember="name";
comboBox.ValueMember="id";
  • 打赏
  • 举报
回复
ufrshchenw 2005-07-08
第一步:public class DataGridComboBoxColumn : DataGridTextBoxColumn
{
public NoKeyUpCombo ColumnComboBox = null;
private System.Windows.Forms.CurrencyManager _source = null;
private int _rowNum;
private bool _isEditing = false;
ComboValueChanged _valueChanging;

public DataGridComboBoxColumn(ComboValueChanged valueChanging) : base()
{
_valueChanging = valueChanging;
ColumnComboBox = new NoKeyUpCombo();

ColumnComboBox.Leave += new EventHandler(LeaveComboBox);
// ColumnComboBox.Enter += new EventHandler(ComboMadeCurrent);
ColumnComboBox.SelectedIndexChanged += new System.EventHandler(ComboIndexChanged);
ColumnComboBox.SelectionChangeCommitted += new System.EventHandler(ComboStartEditing);

}

private void ComboStartEditing(object sender, EventArgs e)
{
_isEditing = true;
base.ColumnStartedEditing((Control) sender);
}

private void ComboIndexChanged(object sender, EventArgs e)
{
_valueChanging(_rowNum , ColumnComboBox.Text);
}

// private void ComboMadeCurrent(object sender, EventArgs e)
// {
// //_isEditing = true;
// }

private void LeaveComboBox(object sender, EventArgs e)
{
if(_isEditing)
{
SetColumnValueAtRow(_source, _rowNum, ColumnComboBox.Text);
_isEditing = false;
Invalidate();
}
ColumnComboBox.Hide();


}
protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{


base.Edit(source,rowNum, bounds, readOnly, instantText , cellIsVisible);

_rowNum = rowNum;
_source = source;

ColumnComboBox.Parent = this.TextBox.Parent;
ColumnComboBox.Location = this.TextBox.Location;
ColumnComboBox.Size = new Size(this.TextBox.Size.Width, ColumnComboBox.Size.Height);
ColumnComboBox.SelectedIndexChanged -= new System.EventHandler(ComboIndexChanged);
ColumnComboBox.Text = this.TextBox.Text;
ColumnComboBox.SelectedIndexChanged += new System.EventHandler(ComboIndexChanged);

this.TextBox.Visible = false;
ColumnComboBox.Visible = true;
ColumnComboBox.BringToFront();
ColumnComboBox.Focus();
}

protected override bool Commit(System.Windows.Forms.CurrencyManager dataSource, int rowNum)
{
if(_isEditing)
{
_isEditing = false;
SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text);
}
return true;
}
第二步:private void AddCustomDataTableStyle()
{
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Customers";
// Set other properties.
ts1.AlternatingBackColor = Color.LightGray;
//
// Add 2 cols with textbox column style
DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custID";
TextCol.HeaderText = "CustomerID";
TextCol.Width = 80;
ts1.GridColumnStyles.Add(TextCol);

TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custName";
TextCol.HeaderText = "Customer Name";
TextCol.Width = 100;
ts1.GridColumnStyles.Add(TextCol);

// Step 2 - Use the combo column style
// Add 1 col with combo style
DataGridComboBoxColumn ComboTextCol = new DataGridComboBoxColumn(new ComboValueChanged(MyComboValueChanged));
ComboTextCol.MappingName = "custCity";
ComboTextCol.HeaderText = "Customer Address";
ComboTextCol.Width = 100;
ts1.GridColumnStyles.Add(ComboTextCol);

// Step 3 - Additional setup for Combo style
// a) make the row height a little larger to handle minimum combo height
ts1.PreferredRowHeight = ComboTextCol.ColumnComboBox.Height + 3;
// b) Populate the combobox somehow. It is a normal combobox, so whatever...
ComboTextCol.ColumnComboBox.Items.Clear();
ComboTextCol.ColumnComboBox.Items.Add("Chicago");
ComboTextCol.ColumnComboBox.Items.Add("Corvallis");
ComboTextCol.ColumnComboBox.Items.Add("Denver");
ComboTextCol.ColumnComboBox.Items.Add("Great Falls");
ComboTextCol.ColumnComboBox.Items.Add("Kansas City");
ComboTextCol.ColumnComboBox.Items.Add("Los Angeles");
ComboTextCol.ColumnComboBox.Items.Add("Raleigh");
ComboTextCol.ColumnComboBox.Items.Add("Washington");


// c) set the dropdown style of the combo...
ComboTextCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList;

//add the custom table style to the datagrid table styles
dataGrid1.TableStyles.Add(ts1);


}
第三步:附值
http://www.syncfusion.com/FAQ/WindowsForms/Default.aspx#44你可以参考一下这个!
  • 打赏
  • 举报
回复
lyb_abiandbel 2005-07-08
这个问题我刚刚自己解决,你真走运,希望能帮助你。
DisplayMember,ValueMember msdn上说的不是很清楚,看这里:
http://community.csdn.net/Expert/topic/4128/4128001.xml?temp=.4159815
  • 打赏
  • 举报
回复
lyb_abiandbel 2005-07-08
comboBox2.DataSource=this.dataSet11.AA_Depa;
comboBox2.DisplayMember="cbmmc";
comboBox2.ValueMember="cbmmc";
this.sqlDataAdapter4.Fill(this.dataSet11);
i=SelectComBo("AA_Depa",this.dataSet11.Tables["zsyd"].Rows[0][6]); //计算该单元在该表中的行号
this.comboBox2.SelectedValue=this.dataSet11.Tables["AA_Depa"].Rows[i]["cbmmc"];//使用ComboBox的SelectedValue属性显示选中的值

public int SelectComBo(string tableName,object obj) //返回某单元Cell在DataTable中的行号
{
int i=0;
for(i=0;i<this.dataSet11.Tables[tableName].Rows.Count;i++)
{
if(this.dataSet11.Tables[tableName].Rows[i][0].ToString()==obj.ToString())
{
break;
}

}
return i;
}
  • 打赏
  • 举报
回复
老戴12345654 2005-07-08
MSDN

' Visual Basic
Private Sub BindComboBox()
ComboBox1.DataSource = DataSet1.Tables("Suppliers")
ComboBox1.DisplayMember = "ProductName"
End Sub

// C#
private void BindComboBox()
{
comboBox1.DataSource = dataSet1.Tables["Suppliers"];
comboBox1.DisplayMember = "ProductName";
}

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/vbcon/html/vbtskdatabindingcomboboxcheckedlistboxorlistboxcontrol.htm
  • 打赏
  • 举报
回复
发帖
C#
加入

10.7w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
申请成为版主
帖子事件
创建了帖子
2005-07-08 01:32
社区公告

让您成为最强悍的C#开发者