This is detailed and thus long. Full code of example is found below.
Thanks in advance for any help sent my way!! -- Tom
In my wonderings about Bindings and DataGridView I came across what
appeared to be an excellent example at the following link >>
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref4/html
/T_System_Data_DataTable.htm
The following details my logic path and I am hopeful someone
recognizes my flawed thinking mode and kicks me in the right
direction.
My attempts to get this example to work included adding References to
the project and adding several "using" statements. I wish the example
had included the necessary using statements if they are indeed needed.
I know that sounds dumb ... but I don't have it compiled yet. :(
The example code looks like it needs to call the MakeDataTables()
method to "Run all of the functions" ... but I don't find this method
call in the example. There is no Main() either! :( ... So I add a
Main() and guess I should call the MakeDataTables() in the Main
containing class' constructor. Right?
But I don't even get that far. That first comment in the example code
(code provided in entirety below) line says to put the following line
in the "Declarations section". Now that makes me feel a special kind
of stupid because I don't know what or where that section is! So I do
a search on "Declaration section" and come up with the following link
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vsxmlsnip
/html/ae3a858e-266e-4ce0-bcc6-6c874062d556.htm
Reading the information in the above leaves me just as confused. :(
I try various guesses at getting it to compile and get an error that
the name 'dataGrid1' does not exist in the current context method.
Now the description of the example promised the tables would be
displayed in a DataGridView ... but at this point I begin a more
detailed search for where the DataGridView is even instantiated. I
don't find it! I had looked for it earlier and missed it .. but
thought I'd compile it anyways. So it is reasonable to guess I need to
create a DataGridView object named dataGrid1? My reasoning being that
the DataTables are to be bound to a DataGridView object.
At this point I have added the following class to the project >>
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using DataTable1;
public class missingClass : Form
{
public DataGridView dataGrid1;
public DataTable1.Program p;
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new missingClass());
}
public missingClass()
{
dataGrid1 = new DataGridView();
p = new Program();
p.MakeDataTables();
}
}
Probably a few too many using statements ... but I've been exploring a
lot and just seem to keep adding more. I'll clean it down to bare
bones later.
Then I get an error that the method call does not have adequate access
privilege. So I place "public" before the "class Program" and am
eyeing that "private" word with concern on the MakeDataTables() method
definition line >>
private void MakeDataTables()
Yep, I am forced to make the above method public.
Now I am down to only one compilation error that is associated with
the following statement >>
dataGrid1.SetDataBinding(dataSet, "ParentTable");
Yikes. Error states 'dataGrid1' does not exist in current context.
Considering all the public access specifications I've used ... all
that is needed is to preface like the following?? >>
missingClass.dataGrid1.SetDataBinding(dataSet, "ParentTable");
This attempted fix above is not working. I must be brain dead by now
because this type of error does not seem too difficult.
Hi Tom,
The examples assumes you have a DataGrid called dataGrid1 and not a
DataGridView, despite clearly saying you need a DataGridView. A DataGridView
does not have a SetDataBinding method.
To get the example working, create an empty windows form and add a DataGrid
called dataGrid1 to it. In the Form's Load event, call MakeAllTables(). Add
the code below to your form as well as the entire example code.
DataGrid dataGrid1;
protected override void OnLoad(EventArgs e)
{
dataGrid1 = new DataGrid();
dataGrid1.Dock = DockStyle.Fill;
this.Controls.Add(dataGrid1);
MakeDataTables();
}
See more answers inline
> This is detailed and thus long. Full code of example is found below.
> Thanks in advance for any help sent my way!! -- Tom
[quoted text clipped - 13 lines]
> had included the necessary using statements if they are indeed needed.
> I know that sounds dumb ... but I don't have it compiled yet. :(
The example needs no more references than what you already have in a default
windows form, though the example fails to mention it is supposed to run
inside such a form.
> The example code looks like it needs to call the MakeDataTables()
> method to "Run all of the functions" ... but I don't find this method
> call in the example. There is no Main() either! :( ... So I add a
> Main() and guess I should call the MakeDataTables() in the Main
> containing class' constructor. Right?
The Main() method is found in Program.cs in a windows project. The call to
MakeDataTables should be done in the form constructor or its load event.
> But I don't even get that far. That first comment in the example code
> (code provided in entirety below) line says to put the following line
> in the "Declarations section". Now that makes me feel a special kind
> of stupid because I don't know what or where that section is! So I do
> a search on "Declaration section" and come up with the following link
The declaration section is basically anywhere inside a class that is outside
a method (or struct, property or enum). Typically this will be near the top
of the class but it doesn't have to be.
> ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vsxmlsnip
> /html/ae3a858e-266e-4ce0-bcc6-6c874062d556.htm
[quoted text clipped - 11 lines]
> create a DataGridView object named dataGrid1? My reasoning being that
> the DataTables are to be bound to a DataGridView object.
You need a DataGrid, not a DataGridView. And to further confuse you, this
control has been removed from the default toolbox in Visual Studio 2005 (and
possibly 2008) and you need to either add it to the ToolBox or add it
manually in code. In my code sample I add it in the Load event in code.
<snip>
> There are several issues at work here. 1) I'd really like to learn how
> to use DataTable and Bind it to a DataGridView. I think that
> combination is a good one to have in one's bag of capabilities.
A few modifications and the example should work :)
> 2) The
> example was anything but "working" and it is still not! If the goal of
> an example is to challenge anyone who might want to explore the
> advertised capabilities with the puzzle of making it work ... mission
> accomplished!
It wasn't very wrong, but it was missing a key piece of information, and it
didn't specifically tell you to put the code inside a windows form.
> 3) The non-working example is eating up a lot of my time
> and anyone who kindly offers to help me out. This is VERY inefficient.
[quoted text clipped - 3 lines]
> up and run it as simply as possible. If that turns out to be the case
> .... I sure wish the example had come with instructions.
Hope this helps.
> BTW, the doco "DataGridView Control Overview" makes the following
> statement >>
>
> "Binding data to the DataGridView control is straightforward and
> intuitive, and in many cases it is as simple as setting the DataSoure
> property."
Well, the DataGridView correctly binds using DataSource and DataMember, but
the example is for a DataGrid.
> Oh how I wish this was true!! Heck, I am still working on compilation.
> I have no clue if this program will work and how many logic flaws
> might be encountered.
The worst error is that the documentation tells you to add a DataGridView.
If it didn't you might recognize the syntax of the control as a DataGrid
(default name is lowercase first letter and add the number 1 at the end,
dataGrid1)
> Can anyone relate to how confusing this is for a newb? Where should I
> begin? How can this even be called an example when it does not meet
[quoted text clipped - 3 lines]
> programmers intentionally include so many bugs or omissions? It can't
> be by accident ... they are too smart for that to be the case.
You'd be amazed how easy it is to introduce bugs. It is also easy to forget
all the little things that are so obvious when you have worked with it for
ages, but other people might never have heard of it. I'll report the bug and
it will hopefully be fixed soon, maybe also with better instructions.

Signature
Happy Coding!
Morten Wennevik [C# MVP]
Tom - 06 Dec 2007 20:49 GMT
Morten --
Thank You so much x 1000000!!!
I've said a working solution that teaches is worth a million words in
reference to a painting worth 1,000. Your kind, generous, accurate,
and VERY knowledgable help is a ton of weight off my shoulders and
worth a million "Thank You"'s.
In the past I hesitated to post so verbosely. Not out of laziness ...
but trying to be concise and respectful of everyone's time. Often I
have worked out the solution before hitting the send button. Putting
the problem in words sometimes helps me sort it all out. I worked a
week ago with DataGrid and that syntax in the example looks very
familiar indeed. I just let that "View" blind me in a sense.
My posting reflects how far out in left field a newb can roam when the
example fails to compile. I do this all-to-often!! There must be a
zillion little microcracks in the sidewalk of programming knowledge
and I seem to find every single one of them!! :(
My desire is that all code snippets will someday be linked in the doco
to fully functional solutions. That way brevity can be maintained in
the specific item of focus and confusion eliminated entirely. Plus a
solid set of examples would not necessarily be so huge as the immense
doco.
If my sincere thank you brightens your day only a fraction of how your
help has brightened mine ... then you are enjoying a very good day and
I wish you endless more.
-- Tom
P.S. I'm studying the form of that example and hopefully learning to
recognize object structural usage patterns. Objects have so much
flexibility that a new-to-me author's style sometimes leads to my
confusion. I am perhaps a bit brain washed after reading a few
authors' texts a few times ... that any deviation in style
discombobulates this newb. An unfortunate side effect of the immense
flexibility of the wonderful C rooted languages. Style certainly
reflects skill and I hope to develop some along this journey.
Sergio Veskovic - 09 Apr 2008 23:40 GMT
I need to use SetDataBinding method,and that is available in dataGrid and not DatagridView control. However I don't see dataGrid in my toolbox so I can't drag it and drop it on my form.
Please help;
Sergio