DataTable Binding with DataGridView -- Doco Example Needs Work

T

Tom

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.
explain to me where I am just simply missing the boat in my attempts
to get this example to work.

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. 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! 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.
There is NO way this example code could have been verified functional
with all the problems that seem apparent to me. Or, perhaps, I have
made this into a disaster by not understanding how to set the example
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.

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."

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.

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
its own promise? Is that not one of the basic object oriented class
derivation rules? Document your advertised performance and provide no
less? But it is ok to provide more? How can the truly expert MS
programmers intentionally include so many bugs or omissions? It can't
be by accident ... they are too smart for that to be the case.

// ##########################################################
Below is the example code from the topmost link above:

Example Description >>

The following example creates two DataTable objects, one DataRelation
object, and adds the new objects to a DataSet. The tables are then
displayed in a DataGridView control.

Example Code Unmodified >>

// Put the next line into the Declarations section.
private System.Data.DataSet dataSet;

private void MakeDataTables()
{
// Run all of the functions.
MakeParentTable();
MakeChildTable();
MakeDataRelation();
BindToDataGrid();
}

private void MakeParentTable()
{
// Create a new DataTable.
System.Data.DataTable table = new DataTable("ParentTable");
// Declare variables for DataColumn and DataRow objects.
DataColumn column;
DataRow row;

// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
column.ReadOnly = true;
column.Unique = true;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);

// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ParentItem";
column.AutoIncrement = false;
column.Caption = "ParentItem";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);

// Make the ID column the primary key column.
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns["id"];
table.PrimaryKey = PrimaryKeyColumns;

// Instantiate the DataSet variable.
dataSet = new DataSet();
// Add the new DataTable to the DataSet.
dataSet.Tables.Add(table);

// Create three new DataRow objects and add
// them to the DataTable
for (int i = 0; i<= 2; i++)
{
row = table.NewRow();
row["id"] = i;
row["ParentItem"] = "ParentItem " + i;
table.Rows.Add(row);
}
}

private void MakeChildTable()
{
// Create a new DataTable.
DataTable table = new DataTable("childTable");
DataColumn column;
DataRow row;

// Create first column and add to the DataTable.
column = new DataColumn();
column.DataType= System.Type.GetType("System.Int32");
column.ColumnName = "ChildID";
column.AutoIncrement = true;
column.Caption = "ID";
column.ReadOnly = true;
column.Unique = true;

// Add the column to the DataColumnCollection.
table.Columns.Add(column);

// Create second column.
column = new DataColumn();
column.DataType= System.Type.GetType("System.String");
column.ColumnName = "ChildItem";
column.AutoIncrement = false;
column.Caption = "ChildItem";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);

// Create third column.
column = new DataColumn();
column.DataType= System.Type.GetType("System.Int32");
column.ColumnName = "ParentID";
column.AutoIncrement = false;
column.Caption = "ParentID";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);

dataSet.Tables.Add(table);

// Create three sets of DataRow objects,
// five rows each, and add to DataTable.
for(int i = 0; i <= 4; i ++)
{
row = table.NewRow();
row["childID"] = i;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 0 ;
table.Rows.Add(row);
}
for(int i = 0; i <= 4; i ++)
{
row = table.NewRow();
row["childID"] = i + 5;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 1 ;
table.Rows.Add(row);
}
for(int i = 0; i <= 4; i ++)
{
row = table.NewRow();
row["childID"] = i + 10;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 2 ;
table.Rows.Add(row);
}
}

private void MakeDataRelation()
{
// DataRelation requires two DataColumn
// (parent and child) and a name.
DataColumn parentColumn =
dataSet.Tables["ParentTable"].Columns["id"];
DataColumn childColumn =
dataSet.Tables["ChildTable"].Columns["ParentID"];
DataRelation relation = new
DataRelation("parent2Child", parentColumn, childColumn);
dataSet.Tables["ChildTable"].ParentRelations.Add(relation);
}

private void BindToDataGrid()
{
// Instruct the DataGrid to bind to the DataSet, with the
// ParentTable as the topmost DataTable.
dataGrid1.SetDataBinding(dataSet,"ParentTable");
}
 
M

Morten Wennevik [C# MVP]

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


Tom said:
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 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

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.
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.

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.
There is NO way this example code could have been verified functional
with all the problems that seem apparent to me. Or, perhaps, I have
made this into a disaster by not understanding how to set the example
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
its own promise? Is that not one of the basic object oriented class
derivation rules? Document your advertised performance and provide no
less? But it is ok to provide more? How can the truly expert MS
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.
 
T

Tom

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.
 
S

Sergio Veskovic

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
 

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