473,748 Members | 10,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UserControl and BindingContext

I have created a UserControl that encapsulates a third party data grid.
My goal was to create my own DataSource and DataMember properties that
forward the binding to the third party grid, then use binding like
normal.

The problem I am running into is that my UserControl ends up with a
different BindingContext then the ParentForm it is contained in and
thus all other controls on the parent form. (I want various controls
on the form to update as I move between rows in the grid).

As far as I can tell the BindingContext of my user control and the
underlying third party grid get set when the DataSource property is
set. The DataSource property is set before the UserControl is added to
the controls collection on the ParentForm.

I would like my UserControl to use the same BindingContext as the
ParentForm. Does anyone know how to accomplish this so that my
DataSource and is connected to the ParentForm BindingContext?
Any help is appreciated, would gladly post code snippets or clarify any
questions.
~ Adam dR.

Nov 21 '06 #1
6 12132
Hi,

In order for the BindingContext to be the same, assuming that the
third-party data-bound control is behaving properly, the DataSource and
DataMember properties of the data-bound control have to be the same as the
values passed to the BindingContext indexer:

yourControl.Dat aMember = dataMember;
yourControl.Dat aSource = dataSource;

// "this" is the Form
context = this.BindingCon text[dataSource, dataMember];

It shouldn't matter whether the DataSource property of the data-bound
control is set before or after being added to the Form's Controls
collection.

If you are using property bindings for controls on the Form then the data
source must be the same but the member can be different:

txtName.DataBin dings.Add("Text ", dataSource, "FirstName" );

The line above will automatically update the Text property with the
"FirstName" value of the current dataSource record.

If the dataSource to which you are binding the UserControl is a DataSet,
then the data member is probably the name of a DataTable. In that case, you
must specify the name of the DataTable as well in the txtName Binding
otherwise the contexts will be different:

txtName.DataBin dings.Add("Text ", dataSource, "People.FirstNa me");

"People" is required since dataSource is only equal to the DataSet, not the
specific data member.

If the dataSource to which you are binding the UserControl is a DataTable,
then "People." isn't necessary in the Binding above and the original example
should work just fine.

The point is that both the data source and data member of the Binding has to
evaluate to the same values being used by the DataSource and DataMember
properties of the UserControl, respectively, with the addition of a column
name such as "FirstName" being allowed in the member as well.

You may want to supply some code if this doesn't help you at all.

"BindingCon text Class"
http://msdn2.microsoft.com/en-us/lib...ngcontext.aspx

--
Dave Sexton

<Me*****@gmail. comwrote in message
news:11******** *************@b 28g2000cwb.goog legroups.com...
>I have created a UserControl that encapsulates a third party data grid.
My goal was to create my own DataSource and DataMember properties that
forward the binding to the third party grid, then use binding like
normal.

The problem I am running into is that my UserControl ends up with a
different BindingContext then the ParentForm it is contained in and
thus all other controls on the parent form. (I want various controls
on the form to update as I move between rows in the grid).

As far as I can tell the BindingContext of my user control and the
underlying third party grid get set when the DataSource property is
set. The DataSource property is set before the UserControl is added to
the controls collection on the ParentForm.

I would like my UserControl to use the same BindingContext as the
ParentForm. Does anyone know how to accomplish this so that my
DataSource and is connected to the ParentForm BindingContext?
Any help is appreciated, would gladly post code snippets or clarify any
questions.
~ Adam dR.

Nov 22 '06 #2
Hi,

<Me*****@gmail. comwrote in message
news:11******** *************@b 28g2000cwb.goog legroups.com...
>I have created a UserControl that encapsulates a third party data grid.
My goal was to create my own DataSource and DataMember properties that
forward the binding to the third party grid, then use binding like
normal.

The problem I am running into is that my UserControl ends up with a
different BindingContext then the ParentForm it is contained in and
thus all other controls on the parent form. (I want various controls
on the form to update as I move between rows in the grid).

As far as I can tell the BindingContext of my user control and the
underlying third party grid get set when the DataSource property is
set. The DataSource property is set before the UserControl is added to
the controls collection on the ParentForm.
If a Controls BindingContext is null (or hasn't been set) then that Control
will look for a parent that has a non-null BindingContext and return that
one. If no parent can be found with a non-null BindingContext then null is
returned.

However both Form and UserControl inherit from ContainerContro l. When
accessing the BindingContext of a ContainerContro l it will create its own if
it was null and can't find one from a parent (instead of returning null).

So in your case the following is happening :
1) 3th party control DataSource/DataMember is being set
2) 3th party control wants to setup DataBinding and asks (itself) for a
BindingContext
3) Since no BindingContext has been explicitly set on the 3th party control,
it will ask the parent Control (which is your UserControl) for one
4) UserControl doesn't have a parent or a BindingContext set, so it creates
one ...

As for workarounds;

- You could explicitly assign the Forms BindingContext to the UserControls
BindingContext at any time, if the 3th party control behaves properly it
should notice this and rebind or

- you could override property BindingContext on your UserControl and make it
behave like a Control (not creating its own BindingContext) , this means
BindingContext will return null until it's added to the ControlCollecti on,
again the 3th party control should behave properly and postpone its
databinding until a valid BindingContext becomes available, eg.:

public class SomeUserControl : UserControl
{
private BindingContext bindingContext;

public override BindingContext BindingContext
{
set
{
bindingContext = value;
OnBindingContex tChanged(EventA rgs.Empty);
}
get
{
// check if a BindingContext has been assigned
if ( bindingContext != null )
return bindingContext;

// check if we have a parent
if ( Parent != null )
return Parent.BindingC ontext;

// otherwise return null
return null;
}
}

protected override void OnParentBinding ContextChanged( EventArgs e)
{
if ( bindingContext == null )
OnBindingContex tChanged(EventA rgs.Empty);
}
}

HTH,
greetings
>
I would like my UserControl to use the same BindingContext as the
ParentForm. Does anyone know how to accomplish this so that my
DataSource and is connected to the ParentForm BindingContext?
Any help is appreciated, would gladly post code snippets or clarify any
questions.
~ Adam dR.

Nov 22 '06 #3
Hi Bart,

Another workaround for when the DataSource property of the UserControl is
set before it's added to the Form's Controls collection is to bind to a
BindingSource object (in the 2.0 framework). The following code works just
fine, binding to a BindingSource, however binding directly to the DataSet or
DataTable doesn't unless DataSource is set after the UserControl is added to
the Controls collection of the Form:

private BindingSource dataSource;
private DataTable table;
private TextBox textBox;

public TestForm() // .ctor for TestForm : Form
{
UserControl userControl = new UserControl();
userControl.Loc ation = new Point(10, 10);
userControl.Siz e = new Size(450, 450);

DataGridView grid = new DataGridView();
grid.Dock = DockStyle.Fill;

userControl.Con trols.Add(grid) ;

DataSet data = new DataSet();
table = data.Tables.Add ();
table.Columns.A dd("String Value", typeof(string)) ;

//grid.DataMember = "Table1";
//grid.DataSource = data;
//grid.DataSource = table;
grid.DataSource = dataSource = new BindingSource(d ata, "Table1");

textBox = new TextBox();
textBox.Locatio n = new System.Drawing. Point(520, 26);
textBox.Size = new System.Drawing. Size(100, 20);

ClientSize = new System.Drawing. Size(712, 556);

Controls.Add(te xtBox);
Controls.Add(us erControl);
}

protected override void OnLoad(EventArg s e)
{
// add test data
for (int i = 1; i < 6; i++)
table.Rows.Add( "Row " + i);

//textBox.DataBin dings.Add("Text ", table.DataSet, "Table1.Str ing
Value");
//textBox.DataBin dings.Add("Text ", table, "String Value");
textBox.DataBin dings.Add("Text ", dataSource, "String Value");

base.OnLoad(e);
}

--
Dave Sexton

"Bart Mermuys" <bm************ *@hotmail.comwr ote in message
news:w6******** **************@ phobos.telenet-ops.be...
Hi,

<Me*****@gmail. comwrote in message
news:11******** *************@b 28g2000cwb.goog legroups.com...
>>I have created a UserControl that encapsulates a third party data grid.
My goal was to create my own DataSource and DataMember properties that
forward the binding to the third party grid, then use binding like
normal.

The problem I am running into is that my UserControl ends up with a
different BindingContext then the ParentForm it is contained in and
thus all other controls on the parent form. (I want various controls
on the form to update as I move between rows in the grid).

As far as I can tell the BindingContext of my user control and the
underlying third party grid get set when the DataSource property is
set. The DataSource property is set before the UserControl is added to
the controls collection on the ParentForm.

If a Controls BindingContext is null (or hasn't been set) then that
Control will look for a parent that has a non-null BindingContext and
return that one. If no parent can be found with a non-null BindingContext
then null is returned.

However both Form and UserControl inherit from ContainerContro l. When
accessing the BindingContext of a ContainerContro l it will create its own
if it was null and can't find one from a parent (instead of returning
null).

So in your case the following is happening :
1) 3th party control DataSource/DataMember is being set
2) 3th party control wants to setup DataBinding and asks (itself) for a
BindingContext
3) Since no BindingContext has been explicitly set on the 3th party
control, it will ask the parent Control (which is your UserControl) for
one
4) UserControl doesn't have a parent or a BindingContext set, so it
creates one ...

As for workarounds;

- You could explicitly assign the Forms BindingContext to the UserControls
BindingContext at any time, if the 3th party control behaves properly it
should notice this and rebind or

- you could override property BindingContext on your UserControl and make
it behave like a Control (not creating its own BindingContext) , this
means BindingContext will return null until it's added to the
ControlCollecti on, again the 3th party control should behave properly and
postpone its databinding until a valid BindingContext becomes available,
eg.:

public class SomeUserControl : UserControl
{
private BindingContext bindingContext;

public override BindingContext BindingContext
{
set
{
bindingContext = value;
OnBindingContex tChanged(EventA rgs.Empty);
}
get
{
// check if a BindingContext has been assigned
if ( bindingContext != null )
return bindingContext;

// check if we have a parent
if ( Parent != null )
return Parent.BindingC ontext;

// otherwise return null
return null;
}
}

protected override void OnParentBinding ContextChanged( EventArgs e)
{
if ( bindingContext == null )
OnBindingContex tChanged(EventA rgs.Empty);
}
}

HTH,
greetings
>>
I would like my UserControl to use the same BindingContext as the
ParentForm. Does anyone know how to accomplish this so that my
DataSource and is connected to the ParentForm BindingContext?
Any help is appreciated, would gladly post code snippets or clarify any
questions.
~ Adam dR.


Nov 22 '06 #4
Hi,

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
Hi Bart,

Another workaround for when the DataSource property of the UserControl is
set before it's added to the Form's Controls collection is to bind to a
BindingSource object (in the 2.0 framework).
Yeah that's true, because a BindingSource owns its own CurrencyManager .

CurrencyManager cm = (CurrencyManage r)someBindingCo ntext[someBindingSour ce];

is the same as:

CurrencyManager cm = someBindingCont ext.CurrencyMan ager;

So it doesn't matter what BindingContext was used, it will always return the
same CurrencyManager .

Greetings
>The following code works just fine, binding to a BindingSource, however
binding directly to the DataSet or DataTable doesn't unless DataSource is
set after the UserControl is added to the Controls collection of the Form:

private BindingSource dataSource;
private DataTable table;
private TextBox textBox;

public TestForm() // .ctor for TestForm : Form
{
UserControl userControl = new UserControl();
userControl.Loc ation = new Point(10, 10);
userControl.Siz e = new Size(450, 450);

DataGridView grid = new DataGridView();
grid.Dock = DockStyle.Fill;

userControl.Con trols.Add(grid) ;

DataSet data = new DataSet();
table = data.Tables.Add ();
table.Columns.A dd("String Value", typeof(string)) ;

//grid.DataMember = "Table1";
//grid.DataSource = data;
//grid.DataSource = table;
grid.DataSource = dataSource = new BindingSource(d ata, "Table1");

textBox = new TextBox();
textBox.Locatio n = new System.Drawing. Point(520, 26);
textBox.Size = new System.Drawing. Size(100, 20);

ClientSize = new System.Drawing. Size(712, 556);

Controls.Add(te xtBox);
Controls.Add(us erControl);
}

protected override void OnLoad(EventArg s e)
{
// add test data
for (int i = 1; i < 6; i++)
table.Rows.Add( "Row " + i);

//textBox.DataBin dings.Add("Text ", table.DataSet, "Table1.Str ing
Value");
//textBox.DataBin dings.Add("Text ", table, "String Value");
textBox.DataBin dings.Add("Text ", dataSource, "String Value");

base.OnLoad(e);
}

--
Dave Sexton

"Bart Mermuys" <bm************ *@hotmail.comwr ote in message
news:w6******** **************@ phobos.telenet-ops.be...
>Hi,

<Me*****@gmail .comwrote in message
news:11******* **************@ b28g2000cwb.goo glegroups.com.. .
>>>I have created a UserControl that encapsulates a third party data grid.
My goal was to create my own DataSource and DataMember properties that
forward the binding to the third party grid, then use binding like
normal.

The problem I am running into is that my UserControl ends up with a
different BindingContext then the ParentForm it is contained in and
thus all other controls on the parent form. (I want various controls
on the form to update as I move between rows in the grid).

As far as I can tell the BindingContext of my user control and the
underlying third party grid get set when the DataSource property is
set. The DataSource property is set before the UserControl is added to
the controls collection on the ParentForm.

If a Controls BindingContext is null (or hasn't been set) then that
Control will look for a parent that has a non-null BindingContext and
return that one. If no parent can be found with a non-null BindingContext
then null is returned.

However both Form and UserControl inherit from ContainerContro l. When
accessing the BindingContext of a ContainerContro l it will create its own
if it was null and can't find one from a parent (instead of returning
null).

So in your case the following is happening :
1) 3th party control DataSource/DataMember is being set
2) 3th party control wants to setup DataBinding and asks (itself) for a
BindingConte xt
3) Since no BindingContext has been explicitly set on the 3th party
control, it will ask the parent Control (which is your UserControl) for
one
4) UserControl doesn't have a parent or a BindingContext set, so it
creates one ...

As for workarounds;

- You could explicitly assign the Forms BindingContext to the
UserControls BindingContext at any time, if the 3th party control behaves
properly it should notice this and rebind or

- you could override property BindingContext on your UserControl and make
it behave like a Control (not creating its own BindingContext) , this
means BindingContext will return null until it's added to the
ControlCollect ion, again the 3th party control should behave properly and
postpone its databinding until a valid BindingContext becomes available,
eg.:

public class SomeUserControl : UserControl
{
private BindingContext bindingContext;

public override BindingContext BindingContext
{
set
{
bindingContext = value;
OnBindingContex tChanged(EventA rgs.Empty);
}
get
{
// check if a BindingContext has been assigned
if ( bindingContext != null )
return bindingContext;

// check if we have a parent
if ( Parent != null )
return Parent.BindingC ontext;

// otherwise return null
return null;
}
}

protected override void OnParentBinding ContextChanged( EventArgs e)
{
if ( bindingContext == null )
OnBindingContex tChanged(EventA rgs.Empty);
}
}

HTH,
greetings
>>>
I would like my UserControl to use the same BindingContext as the
ParentForm. Does anyone know how to accomplish this so that my
DataSource and is connected to the ParentForm BindingContext?
Any help is appreciated, would gladly post code snippets or clarify any
questions.
~ Adam dR.



Nov 22 '06 #5
Hi Bart,

Interesting, thanks.

--
Dave Sexton

"Bart Mermuys" <bm************ *@hotmail.comwr ote in message
news:Od******** ******@TK2MSFTN GP03.phx.gbl...
Hi,

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
>Hi Bart,

Another workaround for when the DataSource property of the UserControl is
set before it's added to the Form's Controls collection is to bind to a
BindingSourc e object (in the 2.0 framework).

Yeah that's true, because a BindingSource owns its own CurrencyManager .

CurrencyManager cm =
(CurrencyManage r)someBindingCo ntext[someBindingSour ce];

is the same as:

CurrencyManager cm = someBindingCont ext.CurrencyMan ager;

So it doesn't matter what BindingContext was used, it will always return
the same CurrencyManager .

Greetings
>>The following code works just fine, binding to a BindingSource, however
binding directly to the DataSet or DataTable doesn't unless DataSource is
set after the UserControl is added to the Controls collection of the Form:

private BindingSource dataSource;
private DataTable table;
private TextBox textBox;

public TestForm() // .ctor for TestForm : Form
{
UserControl userControl = new UserControl();
userControl.Loc ation = new Point(10, 10);
userControl.Siz e = new Size(450, 450);

DataGridView grid = new DataGridView();
grid.Dock = DockStyle.Fill;

userControl.Con trols.Add(grid) ;

DataSet data = new DataSet();
table = data.Tables.Add ();
table.Columns.A dd("String Value", typeof(string)) ;

//grid.DataMember = "Table1";
//grid.DataSource = data;
//grid.DataSource = table;
grid.DataSource = dataSource = new BindingSource(d ata, "Table1");

textBox = new TextBox();
textBox.Locatio n = new System.Drawing. Point(520, 26);
textBox.Size = new System.Drawing. Size(100, 20);

ClientSize = new System.Drawing. Size(712, 556);

Controls.Add(te xtBox);
Controls.Add(us erControl);
}

protected override void OnLoad(EventArg s e)
{
// add test data
for (int i = 1; i < 6; i++)
table.Rows.Add( "Row " + i);

//textBox.DataBin dings.Add("Text ", table.DataSet, "Table1.Str ing
Value");
//textBox.DataBin dings.Add("Text ", table, "String Value");
textBox.DataBin dings.Add("Text ", dataSource, "String Value");

base.OnLoad(e);
}

--
Dave Sexton

"Bart Mermuys" <bm************ *@hotmail.comwr ote in message
news:w6******* *************** @phobos.telenet-ops.be...
>>Hi,

<Me*****@gmai l.comwrote in message
news:11****** *************** @b28g2000cwb.go oglegroups.com. ..
I have created a UserControl that encapsulates a third party data grid.
My goal was to create my own DataSource and DataMember properties that
forward the binding to the third party grid, then use binding like
normal.

The problem I am running into is that my UserControl ends up with a
different BindingContext then the ParentForm it is contained in and
thus all other controls on the parent form. (I want various controls
on the form to update as I move between rows in the grid).

As far as I can tell the BindingContext of my user control and the
underlying third party grid get set when the DataSource property is
set. The DataSource property is set before the UserControl is added to
the controls collection on the ParentForm.

If a Controls BindingContext is null (or hasn't been set) then that
Control will look for a parent that has a non-null BindingContext and
return that one. If no parent can be found with a non-null
BindingContex t then null is returned.

However both Form and UserControl inherit from ContainerContro l. When
accessing the BindingContext of a ContainerContro l it will create its
own if it was null and can't find one from a parent (instead of
returning null).

So in your case the following is happening :
1) 3th party control DataSource/DataMember is being set
2) 3th party control wants to setup DataBinding and asks (itself) for a
BindingContex t
3) Since no BindingContext has been explicitly set on the 3th party
control, it will ask the parent Control (which is your UserControl) for
one
4) UserControl doesn't have a parent or a BindingContext set, so it
creates one ...

As for workarounds;

- You could explicitly assign the Forms BindingContext to the
UserControl s BindingContext at any time, if the 3th party control
behaves properly it should notice this and rebind or

- you could override property BindingContext on your UserControl and
make it behave like a Control (not creating its own BindingContext) ,
this means BindingContext will return null until it's added to the
ControlCollec tion, again the 3th party control should behave properly
and postpone its databinding until a valid BindingContext becomes
available, eg.:

public class SomeUserControl : UserControl
{
private BindingContext bindingContext;

public override BindingContext BindingContext
{
set
{
bindingContext = value;
OnBindingContex tChanged(EventA rgs.Empty);
}
get
{
// check if a BindingContext has been assigned
if ( bindingContext != null )
return bindingContext;

// check if we have a parent
if ( Parent != null )
return Parent.BindingC ontext;

// otherwise return null
return null;
}
}

protected override void OnParentBinding ContextChanged( EventArgs e)
{
if ( bindingContext == null )
OnBindingContex tChanged(EventA rgs.Empty);
}
}

HTH,
greetings


I would like my UserControl to use the same BindingContext as the
ParentForm . Does anyone know how to accomplish this so that my
DataSource and is connected to the ParentForm BindingContext?
Any help is appreciated, would gladly post code snippets or clarify any
questions.
~ Adam dR.



Nov 22 '06 #6

"Bart Mermuys" <bm************ *@hotmail.comwr ote in message
news:Od******** ******@TK2MSFTN GP03.phx.gbl...
Hi,

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
>Hi Bart,

Another workaround for when the DataSource property of the UserControl is
set before it's added to the Form's Controls collection is to bind to a
BindingSourc e object (in the 2.0 framework).

Yeah that's true, because a BindingSource owns its own CurrencyManager .

CurrencyManager cm =
(CurrencyManage r)someBindingCo ntext[someBindingSour ce];

is the same as:

CurrencyManager cm = someBindingCont ext.CurrencyMan ager;
argh, silly typo, should read:

CurrencyManager cm = someBindingSour ce.CurrencyMana ger;

>
So it doesn't matter what BindingContext was used, it will always return
the same CurrencyManager .

Greetings
>>The following code works just fine, binding to a BindingSource, however
binding directly to the DataSet or DataTable doesn't unless DataSource is
set after the UserControl is added to the Controls collection of the Form:

private BindingSource dataSource;
private DataTable table;
private TextBox textBox;

public TestForm() // .ctor for TestForm : Form
{
UserControl userControl = new UserControl();
userControl.Loc ation = new Point(10, 10);
userControl.Siz e = new Size(450, 450);

DataGridView grid = new DataGridView();
grid.Dock = DockStyle.Fill;

userControl.Con trols.Add(grid) ;

DataSet data = new DataSet();
table = data.Tables.Add ();
table.Columns.A dd("String Value", typeof(string)) ;

//grid.DataMember = "Table1";
//grid.DataSource = data;
//grid.DataSource = table;
grid.DataSource = dataSource = new BindingSource(d ata, "Table1");

textBox = new TextBox();
textBox.Locatio n = new System.Drawing. Point(520, 26);
textBox.Size = new System.Drawing. Size(100, 20);

ClientSize = new System.Drawing. Size(712, 556);

Controls.Add(te xtBox);
Controls.Add(us erControl);
}

protected override void OnLoad(EventArg s e)
{
// add test data
for (int i = 1; i < 6; i++)
table.Rows.Add( "Row " + i);

//textBox.DataBin dings.Add("Text ", table.DataSet, "Table1.Str ing
Value");
//textBox.DataBin dings.Add("Text ", table, "String Value");
textBox.DataBin dings.Add("Text ", dataSource, "String Value");

base.OnLoad(e);
}

--
Dave Sexton

"Bart Mermuys" <bm************ *@hotmail.comwr ote in message
news:w6******* *************** @phobos.telenet-ops.be...
>>Hi,

<Me*****@gmai l.comwrote in message
news:11****** *************** @b28g2000cwb.go oglegroups.com. ..
I have created a UserControl that encapsulates a third party data grid.
My goal was to create my own DataSource and DataMember properties that
forward the binding to the third party grid, then use binding like
normal.

The problem I am running into is that my UserControl ends up with a
different BindingContext then the ParentForm it is contained in and
thus all other controls on the parent form. (I want various controls
on the form to update as I move between rows in the grid).

As far as I can tell the BindingContext of my user control and the
underlying third party grid get set when the DataSource property is
set. The DataSource property is set before the UserControl is added to
the controls collection on the ParentForm.

If a Controls BindingContext is null (or hasn't been set) then that
Control will look for a parent that has a non-null BindingContext and
return that one. If no parent can be found with a non-null
BindingContex t then null is returned.

However both Form and UserControl inherit from ContainerContro l. When
accessing the BindingContext of a ContainerContro l it will create its
own if it was null and can't find one from a parent (instead of
returning null).

So in your case the following is happening :
1) 3th party control DataSource/DataMember is being set
2) 3th party control wants to setup DataBinding and asks (itself) for a
BindingContex t
3) Since no BindingContext has been explicitly set on the 3th party
control, it will ask the parent Control (which is your UserControl) for
one
4) UserControl doesn't have a parent or a BindingContext set, so it
creates one ...

As for workarounds;

- You could explicitly assign the Forms BindingContext to the
UserControl s BindingContext at any time, if the 3th party control
behaves properly it should notice this and rebind or

- you could override property BindingContext on your UserControl and
make it behave like a Control (not creating its own BindingContext) ,
this means BindingContext will return null until it's added to the
ControlCollec tion, again the 3th party control should behave properly
and postpone its databinding until a valid BindingContext becomes
available, eg.:

public class SomeUserControl : UserControl
{
private BindingContext bindingContext;

public override BindingContext BindingContext
{
set
{
bindingContext = value;
OnBindingContex tChanged(EventA rgs.Empty);
}
get
{
// check if a BindingContext has been assigned
if ( bindingContext != null )
return bindingContext;

// check if we have a parent
if ( Parent != null )
return Parent.BindingC ontext;

// otherwise return null
return null;
}
}

protected override void OnParentBinding ContextChanged( EventArgs e)
{
if ( bindingContext == null )
OnBindingContex tChanged(EventA rgs.Empty);
}
}

HTH,
greetings


I would like my UserControl to use the same BindingContext as the
ParentForm . Does anyone know how to accomplish this so that my
DataSource and is connected to the ParentForm BindingContext?
Any help is appreciated, would gladly post code snippets or clarify any
questions.
~ Adam dR.



Nov 22 '06 #7

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

Similar topics

0
2058
by: zee | last post by:
Hi, Could anybody please tell me why my web application refuses when I use BindingContext. I am selecting from a database and if (this.BindingContext.Count==0) Server.Transfer("Anotherpage.aspx"); SO that it goes to another page if there were no records meeting the select criteria but the error I get is C:\Inetpub\wwwroot\FrontPage\Result_id_search.aspx.cs(157):
0
1904
by: Peter | last post by:
Hi, I'm developing a program in MDI style I've got 2 froms MDIForm (parent form) Form2 (child form with dataGrid1 and myDataSet)
1
2027
by: Rakesh | last post by:
Using the BindingContext we will be able to find the actual position of the row selected in a DataView (regardless of the sort used). What I want to do is exactly the reverse. That is, I know the actual position of the row in the BindingContext, and from that I want to know the position of the same in a dataview/datagrid. Is this possible? The reason why I want to do this is becuase of the
1
3320
by: D. Yates | last post by:
Hi, I retrieved the employee table from the Pubs database into a single dataset called, dataSet12. I dropped two textbox controls and a datagrid control onto the same form and bound the controls at design time to dataSet12's employee table. Now I want to move through the data by pressing a button.... This code will change the data in the text boxes: this.BindingContext.Position += 1;
2
1640
by: John Celmer | last post by:
Can someone please help me with my code. I have a datasetX with two tables, tableA and tableB in the datasetX. The tables have a DataRelation named A_B_Relation. How can I get the BindingManagerBase to be a Currency Manager so that I can get the current position when there is movement in dataGrid1. This is my code: // set up the binding contexts for Master/Detail grids dataGrid1->DataSource = datasetX; dataGrid1->DataMember =...
0
1824
by: Nathan Carroll | last post by:
Attempting to eliminate flicker showing (form) and hiding seems to have no effect as I have it below. if I move the lines: Me.Show() Me.ShowInTaskbar = True f.Close() to the end of the load procedure the flicker goes away and produces effects that destroy my multiple binding scheme. This Line: Me.CurrentDate.Text = CStr(Now) is has a problem with
1
7573
by: Edwin | last post by:
Hi, I created a dataform with the data form wizard which created some textboxes and 1 checkbox. When I add a row (see section "Add Row" for code) the newly added is not accessible. A label "lblNavLocation" generated by the wizard shows that a row has been added but I can not navigate to it. See section "PositionChanged" for code.
0
1088
by: Donald | last post by:
Mostly, I get the currencymanager from the form.bindingcontext property. but I found that I can aslo get the same currencymanager from a textbox.bindingcontext, even though this textbox does not have any bindings assoicated with this control. Why? // form currencymanager cm1 = (CurrencyManager) this.bindingcontext // textbox currencymanager, In fact, textbox1 does't have any databindings. cm2= (CurrencyManager)...
0
1395
by: nina98765 | last post by:
Hi all I have a data-bound Windows form bound to a strongly-typed dataset. Upon opening the form, I would like to Find a specific record based on a parameter I've passed to the form, and display the data in the appropriate textboxes, etc. I would also like to have the entire table opened (i.e., not just the one record I've found) as I would like to navigate through the records My table is called "AP_TBL_Vendors. The problem is that I...
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.