更改组合框的ListItem文本而不更改其值

时间:2016-03-10 22:11:44

标签: c# asp.net lambda combobox listitem

我的客户要求我将描述性文字添加到组合框的下拉列表中。我有一部分正常工作,但数据没有正确加载回组合框。我确定这是因为当在ListItem.Value属性上设置描述性文本时,会传播到Value。但我不希望 public static List<ListItem> MakeDescriptiveList() { List<ListItem> items = new List<ListItem>(); items.Add(new ListItem(string.Empty)); for (double i = 1.0; i > -1E-9; i -= 0.05) //Roundoff error discovered! { string value = i.ToString("0.0#"); items.Add(new ListItem(value)); } items.Find(x => x.Value == "1.0").Text = "1.0\t Foo foo foo"; items.Find(x => x.Value == "0.9").Text = "0.9\t Bar bar bar"; items.Find(x => x.Value == "0.8").Text = "0.8\t Baz baz baz"; ///intermediate items skipped items.Find(x => x.Value == "0.0").Text = "0.0\t FAIL"; return items; 改变。

我的代码:

ComboBox1.SelectedValue = value

在加载数据时,代码会尝试设置value,其中Text是&#34; 1.0&#34;转换为字符串时。但由于ValueText = db.Text都不匹配,因此失败。

在组合框中设置描述性文字的更好方法是什么,而不更改选择的值?

(我的长期意图,超出当前版本的积压,是将所有这些都移到数据库中。然后我可以设置Value = db.Value后跟 If TextBox3.Text = "" Then MsgBox("Please fill in Car Model", MsgBoxStyle.Information) Else 'This checks if the Date Recieved and Date Due is in the correct format If IsDate(TextBox2.Text) And IsDate(TextBox6.Text) Then 'This line takes all the input from the boxes from adding a new job form, and puts it into 'corresponding boxes in the main menu = because only those boxes 'are connected to the database 'It uses the data adapter from form1 Form1.T_JobsTableAdapter.Insert(Me.TextBox2.Text, Me.TextBox3.Text, Me.TextBox4.Text, Me.TextBox5.Text, Me.TextBox6.Text, Me.TextBox7.Text, Me.TextBox8.Text) 'This line updates the table Jobs dataset with the information in the boxes in main menu 'The dataset passes on the collected data to the database Form1.T_JobsTableAdapter.Fill(Form1.JobsDBDataSet.T_Jobs) MsgBox("Record added sucessfully") 'These lines clear the textboxes so that next job can be added TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" TextBox5.Text = "" TextBox6.Text = "" TextBox7.Text = "" TextBox8.Text = "" Else MsgBox("Please ensure the Date Recieved and Date Due is in dd/mm/yyyy format", MsgBoxStyle.Information) End If End If ,所有这些都可以作为我想要。)

1 个答案:

答案 0 :(得分:0)

items.Add(new ListItem(value))行设置新Text的{​​{1}}。由于ListItem为空,查询Value会返回Value

我通过在构造函数Text中明确设置Value来解决这个问题。然后正确更改items.Add(new ListItem(value, value))不会影响Text