我有一个名为dataGridView列的组合框,我可以将comboBox中显示的项目的文本更改为我想要的任何文本吗?
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
if (dataGridView1.Columns[i].ValueType == typeof(string) &&
i != 6 &&
i != 7 &&
i != 8 &&
i != 9)
comboBox1.Items.Add(dataGridView1.Columns[i].Name);
}
comboBox1.SelectedIndex = 0;
答案 0 :(得分:4)
如果您要使用的值不适合作为组合框中的文本,我通常会这样做:
public class ComboBoxItem<T> {
public string FriendlyName { get; set; }
public T Value { get; set; }
public ComboBoxItem(string friendlyName, T value) {
FriendlyName = friendlyName;
Value = value;
}
public override string ToString() {
return FriendlyName;
}
};
// ...
List<ComboBoxItem<int>> comboBoxItems = new List<ComboBoxItem<int>>();
for (int i = 0; i < 10; i++) {
comboBoxItems.Add(new ComboBoxItem<int>("Item " + i.ToString(), i));
}
_comboBox.DisplayMember = "FriendlyName";
_comboBox.ValueMember = "Value";
_comboBox.DataSource = comboBoxItems;
_comboBox.SelectionChangeCommitted += (object sender, EventArgs e) => {
Console.WriteLine("Selected Text:" + _comboBox.SelectedText);
Console.WriteLine("Selected Value:" + _comboBox.SelectedValue.ToString());
};
答案 1 :(得分:4)
我希望这会对你有所帮助:
combobox.Items[combobox.FindStringExact("string value")] = "new string value";
FindStringExact
方法返回特定项文本的索引,因此这是更改Combobox
项文本的更好方法。
注意:这在C#上运行良好。
答案 2 :(得分:1)
试试这个:
ListItem item = comboBox1.Items.FindByText("<Text>");
if (item != null)
{
item.Text = "<New Text>";
}
答案 3 :(得分:0)
可能不是更改文本,而是从特定索引中删除项目并在新文本中使用新文本插入新项目
答案 4 :(得分:0)
您只需通过以下方式更改组合框项目文字:
my_combo.Items [i_index] = "some string";
答案 5 :(得分:0)
这样做的最好方法就是这样:
ui->myComboBox->setItemText(index,"your text");