|
-
Dec 5th, 2005, 01:32 PM
#1
Thread Starter
New Member
empty record in combo box
I need to add an empty record at the begining of a databound combo box.
MTmace
-
Dec 5th, 2005, 02:33 PM
#2
Re: empty record in combo box
is it just to have the combobox be blank when the user gets to it?
-
Dec 5th, 2005, 05:48 PM
#3
Re: empty record in combo box
A data-bound control displays whatever its DataSource contains. If there is no blank entry in your DataSource then there is no blank entry in your ComboBox, period. This sounds like a hack anyway. As klienma intimated, I'm guessing that you don't actually want yuor user to select a blank entry, but rather you want to have the ability to selct no entry at all. To do this you need to set the SelectedIndex to -1. There is a bug that requires you to do this twice when the ComboBox is data-bound. If you want the user to be able to clear their selection, what I always do is to handle the KeyDown event for the ComboBox and test for the Delete key. When the user depresses the Delete key I set the SelectedIndex to -1 (twice).
-
Dec 5th, 2005, 07:10 PM
#4
Thread Starter
New Member
Re: empty record in combo box
I tried this and it did not work.
With Me.cboBox
.Items.Clear()
.DataSource = dvView
.DisplayMember = "field1"
.ValueMember = "field1"
.SelectedIndex = -1
.SelectedIndex = -1
End With
Is this what you were talking about?
MTmace
-
Dec 5th, 2005, 07:30 PM
#5
Re: empty record in combo box
It works for me. In fact, despite the fact that I've read that you have to set the SelectedIndex twice when your ComboBox is data-bound, the following code produced an empty ComoBox for me:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("Col1", GetType(Integer))
dt.Columns.Add("Col2", GetType(String))
For i As Integer = 1 To 10
Dim dr As DataRow = dt.NewRow()
dr(0) = i
dr(1) = "Row " & i.ToString()
dt.Rows.Add(dr)
Next
Dim dv As New DataView(dt)
Me.ComboBox1.DataSource = dv
Me.ComboBox1.DisplayMember = "Col2"
Me.ComboBox1.ValueMember = "Col1"
Me.ComboBox1.SelectedIndex = -1
End Sub
By the way, your calling Items.Clear is redundant because setting the DataSorce excludes any other items anyway. Also, it may be unnecessary to us a DataView as well, which I assume dvView is. All DataTables are associated with a DataView already through their DefaultView property. You would only have to specifically create a DataView if you wanted to be able to filter or sort it without affecting the DefaultView. Also, when you bind a DataTable to a control it is actually the contents of the DefaultView that is displayed. This means that if you filter or sort the DefaultView, the changes will be reflected in the control. All this means that you would usually just bind the DataTable itself to your control, except in very specific circumstances.
-
Dec 5th, 2005, 07:59 PM
#6
Thread Starter
New Member
Re: empty record in combo box
That is interesting. I copy and pasted your code into my form and the default value was Row1.
I am using a dataview because I use the same datatable through out my form in other combo boxes. If I do not use dataviews then when I make a change to one combo box all the other values will change to what ever was last selected.
MTmace
-
Dec 5th, 2005, 08:24 PM
#7
Re: empty record in combo box
 Originally Posted by MTmace
That is interesting. I copy and pasted your code into my form and the default value was Row1.
I am using a dataview because I use the same datatable through out my form in other combo boxes. If I do not use dataviews then when I make a change to one combo box all the other values will change to what ever was last selected.
MTmace
That sounds like legitimate use of DataViews. I just thought I'd mention it because I've seen many people use them unnecessarily. As for why the code I used works for me and not you, . I tested it in VS.NET 2003 on .NET 1.1 SP1.
-
Dec 8th, 2005, 06:10 PM
#8
Thread Starter
New Member
Re: empty record in combo box
I am still having problems with this issue. I tried this with a new project without a problem. But still will not work with my main project.
Weird
More info:
The combobox that I am having trouble with is in a tab control. If I move the combo box off of the tab control the above code does work.
Why would the location of the combo box matter and how can I fix this problem?
It is not that it is in a tab control that causes the problem. The combo box can not be in on the first tab.
MTmace
Last edited by MTmace; Dec 8th, 2005 at 06:29 PM.
-
Dec 8th, 2005, 06:42 PM
#9
Thread Starter
New Member
Re: empty record in combo box
This codes works:
Me.TabControl1.SelectedTab = TabPage2
Dim dt As New DataTable
dt.Columns.Add("Col1", GetType(Integer))
dt.Columns.Add("Col2", GetType(String))
For i As Integer = 1 To 10
Dim dr As DataRow = dt.NewRow()
dr(0) = i
dr(1) = "Row " & i.ToString()
dt.Rows.Add(dr)
Next
Dim dv As New DataView(dt)
Me.ComboBox1.DataSource = dv
Me.ComboBox1.DisplayMember = "Col2"
Me.ComboBox1.ValueMember = "Col1"
Me.ComboBox1.SelectedIndex = -1
I guess you need to have the page selected before you can access or modify certain methods.
Another problem arises when I try to add
Me.TabControl1.SelectedTab = TabPage1
at the end of the code to have the initial page show.
Is there a better way to handle this problem?
MTmace
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|