【发布时间】:2013-05-21 05:53:24
【问题描述】:
我正在尝试在 SelectedIndex 的基础上设置 ComboBox 的 Text 属性,但问题是 Text 在更改 Combobox 的索引后变为 String.Empty。
ComboBox 中的每个 Item 对应于 DataTable 中的一个字符串,具有 2 列 Name、Description
当我想在ComboBox 中显示该名称的描述时,我需要的是用户选择名称(索引更改)
我尝试过的:
private void tbTag_SelectionChangeCommitted(object sender, EventArgs e)
{
// get the data for the selected index
TagRecord tag = tbTag.SelectedItem as TagRecord;
// after getting the data reset the index
tbTag.SelectedIndex = -1;
// after resetting the index, change the text
tbTag.Text = tag.TagData;
}
我如何填充组合框
//load the tag list
DataTable tags = TagManager.Tags;
foreach (DataRow row in tags.Rows)
{
TagRecord tag = new TagRecord((string)row["name"], (string)row["tag"]);
tbTag.Items.Add(tag);
}
使用的助手类:
private class TagRecord
{
public TagRecord(string tagName, string tagData)
{
this.TagName = tagName;
this.TagData = tagData;
}
public string TagName { get; set; }
public string TagData { get; set; }
public override string ToString()
{
return TagName;
}
}
【问题讨论】:
标签: c# .net vb.net winforms combobox