Solved

I do not view what I need in my Datagrid

Posted on 2003-12-08
16
605 Views
Last Modified: 2011-09-20
Hello,
here is my code, It is very simple, only one table, I want to show the table content in the Datagrid.
It currently works, but I need to click on a "+" sign, then, the word "table" and then I got my "item table" and the content.

How come?  My       dataGrid1.DataMember is Setted.

How to I get only the table content to be displayed?

Thanks!




                  string _myConnStr = "Data Source=eb.mdb;Provider=Microsoft.Jet.OLEDB.4.0;";
                  string _mysqlstr = "SELECT * FROM item ORDER BY ordernum";
                        try
                        {
                              myOledbConn = new OleDbConnection(_myConnStr);
                              myOledbConn.Open();
                              Console.WriteLine("\nmyOledbConn Open...");
                        }
                        catch(Exception ex)
                        {
                              MessageBox.Show("Error: Failed to create a database connection. \n{0}" + ex.Message);
                              Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
                              return;
                        }

                  selectCMD = new OleDbCommand(_mysqlstr,myOledbConn);
                  myitemDA = new OleDbDataAdapter(selectCMD);
                  myitemDS = new DataSet();
                  myOledbConn.Close();
                  Console.WriteLine("Dataset errors?: " + myitemDS.HasErrors);
                  dataGrid1.DataMember = "item";
                  dataGrid1.DataSource = myitemDS;
                  myitemDS.Clear();
                  myitemDA.Fill(myitemDS);                  
            }
0
Comment
  • 7
  • 6
  • 3
16 Comments
 
LVL 4

Expert Comment

by:Tonylmiller
ID: 9898282
I normally do it like this:

DataSet GridDS = BuildGridDataSet(); //This method fills the table with data.
dataGrid1.SetDataBinding(GridDS, "TableName");

I normally build my dataset before binding, as you can see.                  

If that doesn't work, look at your dataset.  Maybe something strange is going on.  From what you have said, it looks like you might have a parent/child relationship going in there.

I use a method to print all table contents to the console, which helps in debugging (unless your table has too many rows).

Of course, you can always set your tablestyle and columstyles to get exactly what you want in your datagrid.

Good luck!
0
 

Author Comment

by:Yann de Champlain, PMP
ID: 9898636
Do you mean you put your OleDbDataAdapter and Fill in your method and return the dataset?

               selectCMD = new OleDbCommand(_mysqlstr,myOledbConn);
               myitemDA = new OleDbDataAdapter(selectCMD);
               myitemDS = new DataSet();
When I change the code I many times get:
Additional information: Cannot create a child list for field item.

My code id now:

                  string _myConnStr = "Data Source=eb.mdb;Provider=Microsoft.Jet.OLEDB.4.0;";
                  string _mysqlstr = "SELECT caption, url FROM item";
                        try
                        {
                              myOledbConn = new OleDbConnection(_myConnStr);
                              myOledbConn.Open();
                              Console.WriteLine("\nmyOledbConn Open...");
                        }
                        catch(Exception ex)
                        {
                              MessageBox.Show("Error: Failed to create a database connection. \n{0}" + ex.Message);
                              Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
                              return;
                        }

                  myitemDA = new OleDbDataAdapter(_mysqlstr,myOledbConn);
                  myitemDS = new DataSet();
                  myitemDA.Fill(myitemDS);
                  dataGrid1.SetDataBinding(myitemDS, "item");
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9899994
You should set the DataMember to the name of the table, namely "ORDER", not "item"
0
 
LVL 4

Assisted Solution

by:Tonylmiller
Tonylmiller earned 100 total points
ID: 9904309
Good point.  I had assumed that "item" was the name of the table.  

It should look just like my example:

dataGrid1.SetDataBinding(GridDS, "TableName");

with the name of the dataset and then the name of the table in double quotes.
0
 

Author Comment

by:Yann de Champlain, PMP
ID: 9904408
item is the table name.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9904442
Based on your code it's ORDER, not item. Change it and see
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9904476
I mean your original code
0
 

Author Comment

by:Yann de Champlain, PMP
ID: 9904797
The only place where ORDER is written is the ORDER BY of the select statement.

Her is a new version of my code without any order by:

string _myConnStr = "Data Source=eb.mdb;Provider=Microsoft.Jet.OLEDB.4.0;";
                  string _mysqlstr = "SELECT caption, url FROM item";
                        try
                        {
                              myOledbConn = new OleDbConnection(_myConnStr);
                              myOledbConn.Open();
                              Console.WriteLine("\nmyOledbConn Open...");
                        }
                        catch(Exception ex)
                        {
                              MessageBox.Show("Error: Failed to create a database connection. \n{0}" + ex.Message);
                              Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
                              return;
                        }
                  myitemDA = new OleDbDataAdapter(_mysqlstr,myOledbConn);
                  myitemDS = new DataSet();
                  myitemDA.Fill(myitemDS);
                  dataGrid1.SetDataBinding(myitemDS, "item");// to change the Datasource and Dataqmember at run time.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 20

Accepted Solution

by:
TheAvenger earned 150 total points
ID: 9904857
OK, sorry, my mistake :-((

By the way: you don't need to open or close the connection. The data adapter will do this alone. Try this code:

string _myConnStr = "Data Source=eb.mdb;Provider=Microsoft.Jet.OLEDB.4.0;";
string _mysqlstr = "SELECT caption, url FROM item";
myOledbConn = new OleDbConnection(_myConnStr);
myitemDA = new OleDbDataAdapter(_mysqlstr,myOledbConn);
myitemDS = new DataSet();
myitemDA.Fill(myitemDS);
dataGrid1.DataMember = myitemDS.Tables[0].TableName;
dataGrid1.DataSource = myitemDS;
0
 
LVL 4

Expert Comment

by:Tonylmiller
ID: 9904879
Not sure if it will create the "item" table for you in your dataset automatically.

Try this:

Create a dataset that contains the item table.  Let's say its called ItemDataSet.xsd

Then do myitemDS = new ItemDataSet(); //in place of your code.

Let me know if it works.  :-)
0
 

Author Comment

by:Yann de Champlain, PMP
ID: 9904941
TheAvenger your code works perfectly.

But, I don't undersand why... since it looks same a my code.

The only diff. is myitemDS.Tables[0].TableName;

Does that mean, the code might have a problem with "item"?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9904960
No, maybe because of the table name. Try to print the table name:

MessageBox.Show (myitemDS.Tables[0].TableName);

and have a look what it is
0
 

Author Comment

by:Yann de Champlain, PMP
ID: 9905027
Great idea...
With a very unpredictable result for me!

The table name is... "Table"!!!

I guest this is because the dataset is filled automatically?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9905041
Probably, yes :-(( I didn't know that
0
 

Author Comment

by:Yann de Champlain, PMP
ID: 9905137
I would like to give points to both of you.
I am not sure how to achieve that.
Do you know how?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9905643
You should have a link called Split Points, below the messages I believe. Then you can decide to whom to give how many points and who is the accepted answer and who is assisted answer.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now