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

已经将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的名称改短一点…………

//应该使用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;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-05-29
labeltest.Text= comboBoxAddNewsCategory.SelectedValue.ToString();

这句加到 comboBoxCategory 的onchange 事件中应该没有问题吧 是不是加错地方了?本回答被提问者采纳

获取comboBox选中的项的值?
一般是这样绑定和取值:comboBox1.DataSource=student;\/\/绑定student表为数据源comboBox1.DisplayMember="name";comboBox1.ValueMember="id";stringname=comboBox1.SelectedText;\/\/取DisplayMember值stringid=comboBox1.SelectedValue.ToString();\/\/取ValueMember值DataTabletable1=this.comboBox1.DataSourceas...

C#如何获取comboBox1的值
this.comboBox1.DataSource=userNameList();\/\/userNameList(),返回List集合.this.comboBox1.DisplayMember="userName";\/\/userName指数据库中的字段名,展现给用户的列this.comboBox1.ValueMember="列名",程序员实际操作的列

c#winform如何讲combobox控件里的数据写入数据库?
在后台CS代码中,可以利用集合的扩展方法来绑定枚举量,这种方法具有很好的通用性,封装好后可以方便地应用于多个场景。另外,还可以通过遍历枚举进行添加,实现数据的绑定。这便是ComboBox枚举量绑定的两种方法的讲解。为了更深入地了解这些方法的实现细节和应用场景,可以参考原文链接:C# ComboBox枚举量绑定...

C# WinForm 如何使combobox 绑定到数据库中某表的一列。即combobox下拉...
\/\/创建一个DataTable用于存储数据,有两列:Name,ValueDataTable dt = new DataTable();dt.Columns.Add("Name");dt.Columns.Add("Value");dt.Rows.Add("请选择", -1);dt.Rows.Add("张三", 1);dt.Rows.Add("李四", 2);dt.Rows.Add("王五", 3); \/\/将Combobox绑定到DataTable,Nam...

C#中怎么获取comboBox下拉列表选中项的Tag值
1、你保存的Tag,应该用一个符号对每个ID进行分割,如:1,2,3string[] arrID=comboBox.Tag.ToString().Split(new string[] {','},System.StringSplitOptions.None);string myID=arrID[comboBox.SelectIndex];这个myID就是你想要的ID,前提是你的下拉框的值的顺序要和ID的顺序一致,否则出错...

C# ComboBox 值绑定??
。object[] obj = { "值1", "值2" };ComboBox.Items.AddRange(obj);一般都是一个name对应一个value,你非要用两个值的话可以在程序一开始定义两个全局变量i,j然后在SelectedIndexChanged中通过判断ComboBoxm.Text的值来确定 比如 if(ComboBoxm.Text.Equals("秋天")){ i=值1;j=值2;} ...

C# winform中如何取得datagridview中的combobox选择的值,取的ValueMemb...
foreach(GridViewRow row in GridView1.Rows){ foreach(Control contr in row.Controls){ ComboBox cbo = contr as ComboBox;if(cbo != null){ \/\/找到咯,给分 记住用cbo.SelectedItem.Text ...} } }

C#中 combox 下拉怎么显示 数据库中的内容
绑定了DateSource的ComboBox是不能用Items.Add方法最近使用了ComboBox控件,简单总结一下有关它的使用(绑定、增加选项、清除):一、ComboBox的绑定 OleDB oledb = new OleDB(); private void Form1_Load(object sender, EventArgs e) ...{ DataTable dt = oledb.Filldatatable("Select ...

c# 如何获得combobox的下拉表中选中项id的值
如果绑定了数据源,你可以把ID设置为ValueMember,然后你可以用combobox.SelectedValue得到值

c# 怎么获取用户在combox选择的Item值
comboBox2.SelectedItem 你这样是设置comboBox2的默认选定的值 要想得到里面的值插入数据库的话 comboBox2.Text 至于数据库的操作我想你应该会吧

相似回答