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");
}
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");
}