Editable ComBox is not work well with data binding. But we can still do something on it. Below is my example.
Code Snippet
private void testComboBox_Validating(object sender, CancelEventArgs e)
{
string text = this.testComboBox.Text.Trim();
if (!string.IsNullOrEmpty(text))
{
foreach (TestDataSet.TestRow row in this.testDataSet.Test)
{
if (row.Name == text) return;
}
this.testDataSet.Test.AddTestRow(text);
this.testComboBox.SelectedIndex = this.testDataSet.Test.Count - 1;
}
}
The TestDataSet is a typed DataSet. The dataset contains a DataTablenamedTest. The TestDataTable contains two columns Id and Name. The Id column is a primary key with AutoIncrement prop set to true. The Name column is a normal column with string typeand AllowDBNull prop is false specified.
ZW2.
Thanks for your reply. I see what you are doing here is updating the combo box's datasource.
Although this is useful to me (so thank you!) I realise that I have not asked the right question. My apologies.
I am having two separate problems that I suspect probably have the same cause - but I can't work out what it is.
First, a bit of background.
The combo box is not bound in the sense that it should update the datasource. However, it has a DataSource drawn from a table in a SQL database similar to the following:
ID Location
1 "UK"
2 "US"
3 "France"
4 "Outer Mongolia"
5 "Outer space"
The user selects the Location field, but the form will actually work with the ID field.
The combo box has DisplayMember set to the Location field, and the ValueMember set to the ID field.
The combo box is displaying the Location field correctly, but I am checking various other field values:
SelectedValue is displaying the corresponding ID field correctly.
SelectedItem is displaying 'System.Data.DataRowView'
SelectedIndex is displaying 'System.Data.DataRowView'
So my first problem is: why am I not getting correct results from the SelectedItem and SelectedIndex properties?
Second problem:
I am trying to use the combobox's FindString method to return an index from the datasource corresponding to what the user is typing in. However, regardless of what string I tell it to look for, FindString always returns 0, even if the string I provide is the first few letters of one of the items.
Can anyone provide any clues as to either of these problems?
Thank you
David