C#中如何获取combobox的值,combobox已经绑定到数据库。

已经将combobox动态绑定到数据库comboBoxCategory.DataSource=caDao.SelctAllCategory();//这里绑定的数据是从数据... 已经将combobox动态绑定到数据库
comboBoxCategory.DataSource = caDao.SelctAllCategory();//这里绑定的数据是从数据库中查找到的表。
comboBoxCategory.DisplayMember = "name";
comboBoxCategory.ValueMember = "id";
在接下来获取combobox的过程中,添加了一个label用于显示获取的combobox的值。使用了如下命令:

labeltest.Text= comboBoxAddNewsCategory.SelectedValue.ToString();
运行结果label只显示combobox下拉列表中第一项的实际值,选择其他项的时候也不能变。请问怎么修改呢?
labeltest.Text= comboBoxAddNewsCategory.SelectedValue.ToString();写错了,应该是
labeltest.Text= comboBoxCategory.SelectedValue.ToString();因为要提问,所以把combobox的名称改短一点…………
展开
 我来答
百度网友fcc2519
2011-05-29 · TA获得超过596个赞
知道小有建树答主
回答量:1102
采纳率:0%
帮助的人:260万
展开全部
//应该使用SelectedIndexChanged事件
//下面是微软的代码,怎么发布事件,怎么取值,都写得很清楚了
//希望对你有帮助,有问题hi我
// Declare comboBox1 as a ComboBox.
internal System.Windows.Forms.ComboBox ComboBox1;

// This method initializes the combo box, adding a large string array
// but limiting the drop-down size to six rows so the combo box doesn't
// cover other controls when it expands.
private void InitializeComboBox()
{
this.ComboBox1 = new System.Windows.Forms.ComboBox();
string[] employees = new string[]{"Hamilton, David", "Hensien, Kari",
"Hammond, Maria", "Harris, Keith", "Henshaw, Jeff D.",
"Hanson, Mark", "Harnpadoungsataya, Sariya",
"Harrington, Mark", "Harris, Keith", "Hartwig, Doris",
"Harui, Roger", "Hassall, Mark", "Hasselberg, Jonas",
"Harnpadoungsataya, Sariya", "Henshaw, Jeff D.",
"Henshaw, Jeff D.", "Hensien, Kari", "Harris, Keith",
"Henshaw, Jeff D.", "Hensien, Kari", "Hasselberg, Jonas",
"Harrington, Mark", "Hedlund, Magnus", "Hay, Jeff",
"Heidepriem, Brandon D."};

ComboBox1.Items.AddRange(employees);
this.ComboBox1.Location = new System.Drawing.Point(136, 32);
this.ComboBox1.MaxDropDownItems = 5;
this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.ComboBox1.Name = "ComboBox1";
this.ComboBox1.Size = new System.Drawing.Size(136, 81);
this.ComboBox1.TabIndex = 0;
this.Controls.Add(this.ComboBox1);

// Associate the event-handling method with the
// SelectedIndexChanged event.
this.ComboBox1.SelectedIndexChanged +=
new System.EventHandler(ComboBox1_SelectedIndexChanged);
}
... // This method is called when the user changes his or her selection.
// It searches for all occurrences of the selected employee's
// name in the Items array and adds the employee's name and
// the number of occurrences to TextBox1.Text.

// CAUTION This code exposes a known bug: If the index passed to the
// FindStringExact(searchString, index) method is the last index
// of the array, the code throws an exception.
private void ComboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{

ComboBox comboBox = (ComboBox) sender;

// Save the selected employee's name, because we will remove
// the employee's name from the list.
string selectedEmployee = (string) ComboBox1.SelectedItem;

int count = 0;
int resultIndex = -1;

// Call the FindStringExact method to find the first
// occurrence in the list.
resultIndex = ComboBox1.FindStringExact(selectedEmployee);

// Remove the name as it is found, and increment the found count.
// Then call the FindStringExact method again, passing in the
// index of the current found item so the search starts there
// instead of at the beginning of the list.
while (resultIndex!=-1)
{
ComboBox1.Items.RemoveAt(resultIndex);
count += 1;
resultIndex = ComboBox1.FindStringExact(selectedEmployee,
resultIndex);
}
// Update the text in Textbox1.
TextBox1.Text = TextBox1.Text+ "\r\n" + selectedEmployee + ": "
+ count;
}
lang2858
2011-05-29 · TA获得超过122个赞
知道小有建树答主
回答量:246
采纳率:0%
帮助的人:130万
展开全部
labeltest.Text= comboBoxAddNewsCategory.SelectedValue.ToString();

这句加到 comboBoxCategory 的onchange 事件中应该没有问题吧 是不是加错地方了?
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式