Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Validating a combo box
 

Validating a combo box

Hi

I'm having difficulty with validating a combo box in .NET. The combo box is bound to a dataset.

I'm testing what happens if the user types something in that's not on the list and then tabs out of the control. I'm using the Validating event, and any attempt to use the SelectedValue or SelectedItem properties when something invalid is typed in crashes the program with a Null Reference exception.

In fact, typing in something that's on the list rather than selecting it from the drop-down will cause the exception. This is silly.

I'm floored. How do I create a bound combo box that won't crash the whole app if the user doesn't pick from the list?

Thanks

David
David Cleave  Sunday, July 01, 2007 2:22 PM
How are you binding the combobox? The default behavior is to only allow the user to select a item from the list with a mouse. Typing does not work.
Ken Tucker  Sunday, July 01, 2007 4:01 PM
I'm using the 'DropDown' DropDownStyle. This means the text portion is editable.

Thanks

David
David Cleave  Sunday, July 01, 2007 5:35 PM

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

David Cleave  Monday, July 02, 2007 11:15 AM

Hi

Here are my answers.

First: If ComboBox is bound to DataTable, the properties SelectedItem and SelectedIndex always return DataRowView. This behaviour is by design. Because ComboBox uses DataView of the DataTable asits data source. In fact, this behaviour is used by all controls.

Second: I don't know the code you write. But here has a link may help.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.combobox.findstring.aspx

Wei Zhou  Tuesday, July 03, 2007 5:56 AM

You can use google to search for other answers

Custom Search

More Threads

• How do i access BindingSource current row ?
• Export window form to doc file c# windows application
• Datagrid Sort Problem
• BindingContext[ds, tableName].Position
• How could I get the column's index of one column by the name of the column?
• Very simple, but two problems...
• DataGridView problem
• Concurency violation: The updateCommand affected 0 of the expected 1 records
• DataGridView Control
• set value to GridView row