Controls binded to datasource

M

MB

Hello!

I have a panel in a form where to add controls, where the properties are loaded from a database, and the properties also are visible in a gridview (from developers express). The strange thing is that when I change the row in the gridview that object will be the visible one, and when checking (for each control...) the controls in the panel it's properties are equal to eachother, which they shouldn't (are not in the database). I think I've missed something with the binding, but I can't figure out what. Below you can see some of my code which loads the controls into the tab:

Regards Magnus

Private Sub LoadControlsTmp()
Dim st As String = "Prospects_Controls"
Dim cmd As New SqlClient.SqlCommand("select * from " & st & " where _TabID=" & cmbTabs.SelectedItem.IntID, cn)
da.SelectCommand = cmd
If ds.Tables.Count > 0 Then ds.Tables.Clear()
da.Fill(ds, st)
Panel3.Controls.Clear()
Dim dv As DataView = ds.Tables(st).DefaultView
GrdControl3.DataSource = dv
Dim drCTRL As DataRowView
For Each drCTRL In dv
Dim obj As New TextBox
obj.DataBindings.Add(New Binding("Text", dv, "CtrlText"))
obj.DataBindings.Add(New Binding("Name", dv, "CtrlName"))
Panel3.Controls.Add(obj)
Next
End Sub
 
Y

Ying-Shen Yu[MSFT]

Hi Magnus,

Are you intending to bind the obj.Text to dv.CtrlText?
if you want to bind the obj.Text to drCTRL["CtrlText"].
you need write like:
obj.DataBindings.Add(new Binding("Text",dv, drCTRL["CtrlText"]);

the third parameter should be the name of the property you want to bind.
In this case, the value of drCTRL["CtrlText"] should be like
"tablename.columnname"

Does it solve your problem?
If you still have problem on it, please reply this thread to let me know.
Thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
Y

Ying-Shen Yu[MSFT]

Hi Magnus,

Are you intending to bind the obj.Text to dv.CtrlText?
if you want to bind the obj.Text to drCTRL["CtrlText"].
you need write like:
obj.DataBindings.Add(new Binding("Text",dv, drCTRL["CtrlText"]);

the third parameter should be the name of the property you want to bind.
In this case, the value of drCTRL["CtrlText"] should be your "columnname"

Does it solve your problem?
If you still have problem on it, please reply this thread to let me know.
Thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
M

MB

Hello, and thanks for your answer, but unfortunately this didn't solve my problem.
I can't bind to drCTRL("CtrlText"), though this includes the text for the control. The column name is CtrlText so I still think this should be the the text in the third parameter (correct me if I'm wrong).

Since it is so hard to explain what I want and how it appears, I did a small web page with the formcode and a picture of the behavior.
http://www.mikla.nu/WrongControlsBehavior/WrongControlsBehavior.htm
Could you please check into that.

Best regards Magnus
 
Y

Ying-Shen Yu[MSFT]

Hi Magnus,

Sorry, I didn't fully understand your problem.
As my understanding now, You want to enumerate the data rows and create
textbox accordingly.
In this situation, we shouldn't use databinding, since it is designed to
populate the values in current row
to the bounded controls, that's why you get this behavior.
In this case, you simply need write as
<code>
For Each r In dt.Rows
Dim obj As New TextBox
obj.Name = r("CtrlName")
obj.Text = r("CtrlText")
obj.Top = r("CtrlTop")
Panel1.Controls.Add(obj)
Next
</code>
Is this what you want?

If it doesn't help, please be free to reply this thread,
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
M

MB

Hello!

I had an idea about using binded controls for this issue, but since this
doesn't work the way I want, I now do the other way, that you recommend.
Thanks for your answer.

Regards Magnus
 
Y

Ying-Shen Yu[MSFT]

Hi Magnus,

Thanks for your reply,
If you have any problem in applying my recommendation , please be free to
reply this thread.
Thanks1

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top