473,221 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

DataBindings.Add

This is admitedly vague. But so is my understanding of the problem.
I'm using this chunk of code:

retControl.DataBindings.Add(ctrlBinding.PropertyNa me,DataSource[i],BindingField);

This resolves to something like:

Control.DataBindings.Add("Text", SomeDataTable.Rows[1], "Priority");

The only difference that I can see is that one of the DataSets is
created by a DataAdapter through the IDE at design time while the second
is instantiated dynamically at run time. In both cases they contain one
DataTable.

When I attempt to accomplish the same task with the dynamcally
instantiated Control it fails. In the code below you will see a line
that calls the function DeepCloneControl. It never fails within
DeepCloneControl. It always fails after it returns. DeepCloneControl
begins by cloning a control. Then it loops through some of its
properties recreating them in the clone. (Some are excluded.) Finally,
it loops through its bindings recreating them in the clone. (This is
where the problem is. If I comment this loop out, it succeeds.) Only
after the function returns, without ever throwing an exception, does it
fail on the line that called DeepCloneControl. This is the message

Cannot bind to property or column <field name> on DataSource.

Within DeepCloneControl you will notice this:
retControl.DataBindings.Add(ctrlBinding.PropertyNa me,
DataSource[i],BindingField);

DataSource[i] refers to an object returned by:
new DataRowCollectionSocket(DataSet.DataTable.Rows);

A DataRowCollectionSocket is defined by:
public interface IDataSocket
{
int Count{get;}
object this[int index]{get;}
}
public class DataRowCollectionSocket: IDataSocket
{
public System.Data.DataRowCollection Data;

public DataRowCollectionSocket(System.Data.DataRowCollect ion data)
{
this.Data = data;
}

public int Count
{
get
{
return Data.Count;
}
}

public object this[int index]
{
get
{
return Data[index];
}
}
}

Here's the significant bit of code:

tempPanel.Controls.Add(DeepCloneControl(c,i));// This is where it
fails.

public Control DeepCloneControl(Control subject, int i)
{
Type ctrlType = subject.GetType();
ConstructorInfo cInfo = ctrlType.GetConstructor(Type.EmptyTypes);
Control retControl = (Control)cInfo.Invoke(null);

foreach(PropertyInfo pInfo in
ctrlType.GetProperties(BindingFlags.Public|Binding Flags.Instance))
{
if (pInfo.CanWrite && (pInfo.PropertyType.IsValueType ||
pInfo.PropertyType.Name == "String"))
{
try
{
switch(pInfo.Name)
{
case "Parent":
break;
case "SelectionStart":
break;
case "SelectedText":
break;
case "SelectionLength":
break;
default:
pInfo.SetValue(retControl,pInfo.GetValue(subject,n ull),null);
break;
}
}
catch(Exception ex)
{
Console.WriteLine("Could not assign the value of {0} to
\nObject:\t{1}\nOf Type:\t{2}\nBecause:\t{3}", pInfo.Name,
subject.Name, ctrlType.Name, ex.Message);//Never happens
}
}
}
foreach(Binding ctrlBinding in subject.DataBindings)
{
try
{
string BindingMember = ctrlBinding.BindingMemberInfo.BindingMember;
string BindingField =
BindingMember.Substring(BindingMember.LastIndexOf( "."));
retControl.DataBindings.Add(ctrlBinding.PropertyNa me,
DataSource[i],BindingField);
}
catch(Exception)
{
MessageBox.Show("Exception thrown at line 139");//Never happens
}
}
return retControl;
}
I know this is a large chunk of code. I apologize for that. Thanks for
any help you may be able to give. I would like to know any and all
reasons why a DataBinding would fail. There has to be a reason for this.
Dec 1 '05 #1
2 9313
Hi,

What looks suspicious is that you are trying to bind to a DataRow. This is
unusual scenario. Usually one binds to a DataSet or a DataTable (in the
former case, the property name must be specified as TableName.ColumnName),
and an instance of CurrencyManager created behind the scenes will manage the
current row for you.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"C Glenn" <no****@domain.com> wrote in message
news:Oq*************@tk2msftngp13.phx.gbl...
This is admitedly vague. But so is my understanding of the problem. I'm
using this chunk of code:

retControl.DataBindings.Add(ctrlBinding.PropertyNa me,DataSource[i],BindingField);

This resolves to something like:

Control.DataBindings.Add("Text", SomeDataTable.Rows[1], "Priority");

The only difference that I can see is that one of the DataSets is created
by a DataAdapter through the IDE at design time while the second is
instantiated dynamically at run time. In both cases they contain one
DataTable.

When I attempt to accomplish the same task with the dynamcally
instantiated Control it fails. In the code below you will see a line that
calls the function DeepCloneControl. It never fails within
DeepCloneControl. It always fails after it returns. DeepCloneControl
begins by cloning a control. Then it loops through some of its properties
recreating them in the clone. (Some are excluded.) Finally, it loops
through its bindings recreating them in the clone. (This is where the
problem is. If I comment this loop out, it succeeds.) Only after the
function returns, without ever throwing an exception, does it fail on the
line that called DeepCloneControl. This is the message

Cannot bind to property or column <field name> on DataSource.

Within DeepCloneControl you will notice this:
retControl.DataBindings.Add(ctrlBinding.PropertyNa me,
DataSource[i],BindingField);

DataSource[i] refers to an object returned by:
new DataRowCollectionSocket(DataSet.DataTable.Rows);

A DataRowCollectionSocket is defined by:
public interface IDataSocket
{
int Count{get;}
object this[int index]{get;}
}
public class DataRowCollectionSocket: IDataSocket
{
public System.Data.DataRowCollection Data;

public DataRowCollectionSocket(System.Data.DataRowCollect ion data)
{
this.Data = data;
}

public int Count
{
get
{
return Data.Count;
}
}

public object this[int index]
{
get
{
return Data[index];
}
}
}

Here's the significant bit of code:

tempPanel.Controls.Add(DeepCloneControl(c,i));// This is where it fails.

public Control DeepCloneControl(Control subject, int i)
{
Type ctrlType = subject.GetType();
ConstructorInfo cInfo = ctrlType.GetConstructor(Type.EmptyTypes);
Control retControl = (Control)cInfo.Invoke(null);

foreach(PropertyInfo pInfo in
ctrlType.GetProperties(BindingFlags.Public|Binding Flags.Instance))
{
if (pInfo.CanWrite && (pInfo.PropertyType.IsValueType ||
pInfo.PropertyType.Name == "String"))
{
try
{
switch(pInfo.Name)
{
case "Parent":
break;
case "SelectionStart":
break;
case "SelectedText":
break;
case "SelectionLength":
break;
default:
pInfo.SetValue(retControl,pInfo.GetValue(subject,n ull),null);
break;
}
}
catch(Exception ex)
{
Console.WriteLine("Could not assign the value of {0} to
\nObject:\t{1}\nOf Type:\t{2}\nBecause:\t{3}", pInfo.Name, subject.Name,
ctrlType.Name, ex.Message);//Never happens
}
}
}
foreach(Binding ctrlBinding in subject.DataBindings)
{
try
{
string BindingMember = ctrlBinding.BindingMemberInfo.BindingMember;
string BindingField =
BindingMember.Substring(BindingMember.LastIndexOf( "."));
retControl.DataBindings.Add(ctrlBinding.PropertyNa me,
DataSource[i],BindingField);
}
catch(Exception)
{
MessageBox.Show("Exception thrown at line 139");//Never happens
}
}
return retControl;
}
I know this is a large chunk of code. I apologize for that. Thanks for
any help you may be able to give. I would like to know any and all
reasons why a DataBinding would fail. There has to be a reason for this.


Dec 1 '05 #2
Yes, I know this is a bit odd, but it works for creating a psuedo
continuous form. Each cloned instance of a template panel contains the
same collection of controls and each one of the controls is bound to the
same fields but within a different row in the rows collection.

Dmytro Lapshyn [MVP] wrote:
Hi,

What looks suspicious is that you are trying to bind to a DataRow. This
is unusual scenario. Usually one binds to a DataSet or a DataTable (in
the former case, the property name must be specified as
TableName.ColumnName), and an instance of CurrencyManager created behind
the scenes will manage the current row for you.

Dec 1 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: akadiri4 | last post by:
I am trying to bind a textbox to a dataset using VB.Net but it doesn't seem to work Here is the code: DS = New DataSet DA = New SqlDataAdapter(StrSQL, StrConn) DA.Fill(DS, "StrSQL")...
0
by: Richard | last post by:
Hi, I am trying to bind a datetime picker control to a column in a DataView and I get this exception: "Object reference not set to an instance of an object." Pseudo code looks like this: ...
0
by: Kyle Rowe | last post by:
Binding binding = new Binding("Text", x, null); textBox1.DataBindings.Add(binding); I wrote the aboe code when the form loads, but when I change the variable x, the textbox does not display the...
0
by: Johann Blake | last post by:
I have a form with textboxes that are bound to a dataset. The dataset contains two tables and some of the textboxes use data from one table while other textboxes use data from the other table. I...
9
by: HAN(removethis)twister | last post by:
I've tried to create 11 textboxes as variables (not actually visible in the program, not in Windows Form Designer) and have set DataBindings to the text properties of the TextBoxes BUT according to...
6
by: Graham Blandford | last post by:
Hi all, I haven't received any replies to a previous question I posted regarding problems I was having with databound textboxes.. so I'll try a different angle... Can anyone point me in the...
0
by: XenReborn | last post by:
Ok this should be simple. I made a form, added a combobox (for selecting items to edit, not for updating fields), several textboxes, a few checkboxes etc. On formshow it connects to my database,...
2
by: marcf | last post by:
Hi everyone, I've just studio using VS2008 and have a quick question regarding databindings, i'm fairly handy with vb6 so this question really relates to good practice rather than actual coding. ...
1
by: janj | last post by:
Hi all, I'm having a problem binding two object properties to properties of another object. The code below works as expected when only one binding is added but doesn't work at all (still compiles)...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.