Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Cast from type 'DataRowView' to type 'String' is not valid.
 

Cast from type 'DataRowView' to type 'String' is not valid.

Hi,
I have 2 forms in windows application (vb.net),mainform and childform where childform acts as a popup window. mainform will have multiple textboxes / comboboxes and a hyperlink. When a user clicks on hyperlink , popup(childform) will open at a specified location. This popup window will have few comboboxes whose values will be selected by the user and will click 'OK' button. To open a popup(childform) by clicking on link am using this code:
[CODE]Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles ll_popup.LinkClicked
Dim NCNopenpopup As New frmNCNtest
NCNopenpopup.StartPosition = FormStartPosition.Manual
NCNopenpopup.Location = New Point(350, 350)
NCNopenpopup.Show()
End Sub[/CODE]I want to know whether to use [CODE]NCNopenpopup.Show() or NCNopenpopup.ShowDialog()[/CODE] ?
Below is property am using to bind to textbox on main form:
[CODE]Public WriteOnly Property _textBox() As String
Set(ByVal Value As String)
Me.txt_test1.Text = Value
End Set
End Property[/CODE]
Now in childform i have textbox where user can enter some text and click on ok button. code is below:
[CODE]Public ReadOnly Property _textBox1() As String
Get
Return txt_test4.Text
End Get
End Property
Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
Dim frm As New frmNCNpopup
frm._textBox = _textBox1
frm.Show()
Me.Close()
End Sub[/CODE]Till here it works fine except that from mainform when a user clicks on hyperlink, enters text on childform and clicks on 'OK' Button in childform, then 'A NEW MAINFORM GETS OPENED' so now there are 2 mainform's. I want to populate on the MainForm's textbox which was opened first and not on new mainform.There should be only one mainform. how to do this?

Secondly, the above was having textbox in childform. I need a combobox in childform and when i try with following code
NCNopenpopup.Show() --> code in hyperlink click event on mainform., it populates the textbox on mainform with this value : System.Data.DataRowView And when i try with this code [CODE]NCNopenpopup.ShowDialog() [/CODE] --> code in hyperlink click event on mainform., it throws error: "Cast from type 'DataRowView' to type 'String' is not valid."
For Combobox in childform am using following code:
[CODE]Public ReadOnly Property _textBox1() As String
Get
Return Me.cbostatus.SelectedItem.ToString() ---> It opens mainform with no values in it (2 forms as told earlier)
Return Me.cbostatus.SelectedItem --> This code when used throws error "Cast from type 'DataRowView' to type 'String' is not valid."
End Get
End Property[/CODE]
Note : --> are my comments when corresponding code is used.

Please help me on this problem.Thanks.

yasinirshad  Sunday, November 18, 2007 2:27 PM

Hi yasinirshad,

Based on your post, you want to return the selected value of your combo box to your main form, don’t you? If so, I suggest use the SelectedValue property or Text property of ComboBox instead, since the SelectedItem returns the current object in the underlying data source (a DataRowView). Try something like the following:

Code Block

Public ReadOnly Property SelectValue() As String

Get

If (Me.ComboBox1.SelectedValue IsNot Nothing) Then

'if you want return the display value, you can use the Text property of combobox instead

Return Me.ComboBox1.SelectedValue.ToString()

Else

Return String.Empty

End If

End Get

Property

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, November 22, 2007 10:53 AM

Hi yasinirshad,

Based on your post, you want to return the selected value of your combo box to your main form, don’t you? If so, I suggest use the SelectedValue property or Text property of ComboBox instead, since the SelectedItem returns the current object in the underlying data source (a DataRowView). Try something like the following:

Code Block

Public ReadOnly Property SelectValue() As String

Get

If (Me.ComboBox1.SelectedValue IsNot Nothing) Then

'if you want return the display value, you can use the Text property of combobox instead

Return Me.ComboBox1.SelectedValue.ToString()

Else

Return String.Empty

End If

End Get

Property

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, November 22, 2007 10:53 AM

You can use google to search for other answers

Custom Search

More Threads

• StopMoving() not stopping moving
• Using Managed SPY++ techniques for MenuClick
• Direct ADO instead of Web Service
• Code Downloads? Animal Farm?
• HTTP status 401: Access Denied
• New version questions
• Has Microsoft officially killed terrarium??
• Wanted someone with a suitable terrarium for herbivore introduction
• Receiving Cretaures
• datagridview column cel value multiply to textbox value