|
-
Feb 10th, 2006, 05:09 AM
#1
Thread Starter
Hyperactive Member
Dataview problems [resolved]
Okay, I have two combo boxes on a form, both displaying a different column from the same datatable, I want both these combo boxes to be sorted alphabetically though, so I created a dataview as so
VB Code:
ds.Tables.Add("ContactsCom")
ddiView = New DataView(ds.Tables("ContactsCom"))
I then bind the combo boxes as follows
VB Code:
Me.cbocompany.DataSource = ds
Me.cbocompany.DisplayMember = "ContactsCom.CompanyName"
Me.cbocompany.ValueMember = "ContactsCom.ContactID"
ddiView.Sort = "MID"
cboddi.DataSource = ddiView
Me.cboddi.DisplayMember = "MID"
Me.cbocompany.ValueMember = "ContactID"
Which all seems to work fine, but my problem now is that on the
cbocompany combo box this works
VB Code:
Private Sub cbocompany_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbocompany.SelectedIndexChanged
Try
MsgBox(cbocompany.SelectedValue)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
But on the cboddi box I get the exception
VB Code:
Private Sub cboddi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboddi.SelectedIndexChanged
Try
MsgBox(cboddi.SelectedValue)
Catch ex As Exception
Me.TextBox1.Text = ex.ToString
End Try
End Sub
System.ArgumentException: Argument 'Prompt' cannot be converted to type 'String'.
Why?
Last edited by Oliver1; Feb 10th, 2006 at 06:51 AM.
Reason: resolved
-
Feb 10th, 2006, 05:30 AM
#2
Re: Dataview problems
First thing that I would like to say is that you should not be using MsgBox function in VB.NET. There is a much better method MessageBox.Show that you should always try to use whenever you need to display a messagebox. MsgBox function is there just for backward compatibility with VB 6.0.
Another thing is that you haven't shown which line is erroring out. Is it the Msgbox function or the catch block that giving you problems.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 10th, 2006, 05:44 AM
#3
Thread Starter
Hyperactive Member
Re: Dataview problems
This statement gives me a value
cbocompany.SelectedValue
but this doesn't
cboddi.SelectedValue
and errors out saying
System.ArgumentException: Argument 'Prompt' cannot be converted to type 'String'.
The msgbox is only there while I'm trying to find the problem, and in fact I want to use the selectedvalue in another function.
I am aware that you shouldn't really use the msgbox function, but then I'm also aware that drinking is bad for you, but that doesn't stop me doing it
-
Feb 10th, 2006, 06:40 AM
#4
Re: Dataview problems
This is a kind of weired problem. I would suggest that before you bind the comboboxes just remove the handlers. And after you are done with you binding stuff add the handlers back. Something similar to this
VB Code:
RemoveHandler cboddi.SelectedIndexChanged, AddressOf cboddi_SelectedIndexChanged
RemoveHandler cbocompany.SelectedIndexChanged, AddressOf cbocompany_SelectedIndexChanged
ds.Tables.Add("ContactsCom")
ddiView = New DataView(ds.Tables("ContactsCom"))
Me.cbocompany.DataSource = ds
Me.cbocompany.DisplayMember = "ContactsCom.CompanyName"
Me.cbocompany.ValueMember = "ContactsCom.ContactID"
ddiView.Sort = "MID"
cboddi.DataSource = ddiView
Me.cboddi.DisplayMember = "MID"
Me.cbocompany.ValueMember = "ContactID"
AddHandler cboddi.SelectedIndexChanged, AddressOf cboddi_SelectedIndexChanged
AddHandler cbocompany.SelectedIndexChanged, AddressOf cbocompany_SelectedIndexChanged
And I have a question for you. When do you actually get this error. Is it when you try to select something in the Combobox or is it before that?
 Originally Posted by Oliver1
The msgbox is only there while I'm trying to find the problem, and in fact I want to use the selectedvalue in another function.
I am aware that you shouldn't really use the msgbox function, but then I'm also aware that drinking is bad for you, but that doesn't stop me doing it
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 10th, 2006, 06:51 AM
#5
Thread Starter
Hyperactive Member
Re: Dataview problems
Thanks for your help, turned out to be a really stupid mistake
Me.cbocompany.ValueMember = "ContactID"
should have been
Me.cboddi.ValueMember = "ContactID"
Hence the reason why it didn't work, must have spent about 2 hours on this.
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
|