Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I do not view what I need in my Datagrid

Posted on 2003-12-08
16
Medium Priority
?
647 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
Question by:Yann de Champlain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Assisted Solution

by:Tonylmiller
Tonylmiller earned 400 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
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
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
 
LVL 20

Accepted Solution

by:
TheAvenger earned 600 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
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
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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

704 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