Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / July 2008

Tip: Looking for answers? Try searching our database.

Code works in form constructor but fails to relocate elsewhere

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mansoor A. Karim - 08 Jul 2008 15:02 GMT
For a form that has two DataGridView controls, I find following piece of
code (free of bindingsource, binding navigator, typed dataset etc
enhancements) conceptually a simple and effective way to display
parent-child relationship. The two grids are synchronized (selecting record
in the parent grid updates records in the child grid) and everything works
ok.

But the problem is that this code only works when I place it in the form
constructor. Any attempt to locate this code in the form load event or a
button click event results in a blank child grid (0 child records in
DataGridView2). This is strange as I fail to see any line in my code that
should/can only appear in constructor. Please guide me on this problem.

Thanks
Mansoor

Code that only works OK in form constructor:
----------------------------------------------

           DataSet dataSet;

           SqlConnection objConnection;
           SqlDataAdapter objDataAdapter;

           DataColumn parentColumn;
           DataColumn childColumn;

           //Open connection
           objConnection = new SqlConnection(connectionString);
           objConnection.Open();

           //Initialize dataset
           dataSet = new DataSet("NorthWindDataset");

           //Create and fill Customers datatable
           objDataAdapter = new SqlDataAdapter("SELECT * FROM Customers",
objConnection);
           objDataAdapter.Fill(dataSet, "Customers");

           //Create and fill Orders datatable
           objDataAdapter = new SqlDataAdapter("SELECT * FROM Orders",
objConnection);
           objDataAdapter.Fill(dataSet, "Orders");

           //Close connection
           objConnection.Close();

           //Create foreign key constraint and relationship between
Customers and Orders datatable
           parentColumn =
dataSet.Tables["Customers"].Columns["CustomerID"];
           childColumn = dataSet.Tables["Orders"].Columns["CustomerID"];
           dataSet.Relations.Add("Customer_Orders", parentColumn,
childColumn);

           //Bind grid to parent records
           dataGridView1.DataMember = "Customers";
           dataGridView1.DataSource = dataSet;

           //Bind grid to child records
           dataGridView2.DataMember = "Customers.Customer_Orders";
           dataGridView2.DataSource = dataSet;
Morten Wennevik [C# MVP] - 09 Jul 2008 07:09 GMT
Hi,

It took me a while to crack this one as indeed it only worked in the
constructor.  However, you have mixed the order of DataSource and DataMember.
Add the DataSource before the DataMember.  Usually this would work fine as
the existance of the DataMember is resolved if you later set the DataSource,
but apparantly not with relations, and the DataMember would consistently get
reset when dataGridView2 got its DataSource when the code ran outside the
constructor.  

The reason why this worked in the constructor is probably due to the fact
that databindings aren't fully implemented until the controls are actually
created, and the DataGridViews aren't created at constructor time (Created
property is false).  When they later got created it didn't really matter if
you added the DataSource or DataMember first as the code for the DataSource
probably ran first anyway.  This is the reason why you shouldn't set up
DataBindings in the constructor.

Signature

Happy Coding!
Morten Wennevik [C# MVP]

> For a form that has two DataGridView controls, I find following piece of
> code (free of bindingsource, binding navigator, typed dataset etc
[quoted text clipped - 58 lines]
>             dataGridView2.DataMember = "Customers.Customer_Orders";
>             dataGridView2.DataSource = dataSet;
Mansoor A. Karim - 10 Jul 2008 04:58 GMT
Thanks Morten for help.

I have checked and indeed the DataMember gets reset when the
DataSource/DataMember sequence is mixed. Just wondering, I never came across
this order significance in the documentations.

I have another similar code relocation problem that I am posting separately
and would appreciate if you could please also crack this one for me.

> Hi,
>
[quoted text clipped - 84 lines]
>>             dataGridView2.DataMember = "Customers.Customer_Orders";
>>             dataGridView2.DataSource = dataSet;
Morten Wennevik [C# MVP] - 10 Jul 2008 07:50 GMT
Hi,

When stepping into the framework code it seems to boild down to this.

When setting DataMember before DataSource, the DataGridView fails to create
a CurrencyManager with a DataBinding for this DataMember since the DataSource
is null.

When setting DataSource, it detects the existing DataMember, but since the
CurrencyManager does not contain a DataBinding for this DataMember, the
DataMember is considered invalid and sets this.DataMember = "";

I haven't checked the code at constructor time, though, as somehow I cannot
load the framework code in my code sample then.

If you have Visual Studio 2008 (not express) you can step into framework
code, including DataGridView code.  See this article or google for "step into
framework code" for more info:  

[Configuring Visual Studio to Debug .NET Framework Source Code
http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-deb
ug-net-framework-source-code.aspx


Signature

Happy Coding!
Morten Wennevik [C# MVP]

> Thanks Morten for help.
>
[quoted text clipped - 93 lines]
> >>             dataGridView2.DataMember = "Customers.Customer_Orders";
> >>             dataGridView2.DataSource = dataSet;
Mansoor A. Karim - 11 Jul 2008 08:28 GMT
Thanks for the link on stepping into framework code.

Unfortunately I am still using VS2005 (not express edition) but am planning
to upgrade to 2008 shortly. Will refer back to the link then as I have
checked and the feature probably does not apply to VS2005.

> Hi,
>
[quoted text clipped - 135 lines]
>> >>             dataGridView2.DataMember = "Customers.Customer_Orders";
>> >>             dataGridView2.DataSource = dataSet;

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.