Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 649
  • Last Modified:

I do not view what I need in my Datagrid

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
Yann de Champlain
Asked:
Yann de Champlain
  • 7
  • 6
  • 3
2 Solutions
 
TonylmillerCommented:
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
 
Yann de ChamplainAssociateAuthor Commented:
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
 
TheAvengerCommented:
You should set the DataMember to the name of the table, namely "ORDER", not "item"
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TonylmillerCommented:
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
 
Yann de ChamplainAssociateAuthor Commented:
item is the table name.
0
 
TheAvengerCommented:
Based on your code it's ORDER, not item. Change it and see
0
 
TheAvengerCommented:
I mean your original code
0
 
Yann de ChamplainAssociateAuthor Commented:
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
 
TheAvengerCommented:
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
 
TonylmillerCommented:
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
 
Yann de ChamplainAssociateAuthor Commented:
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
 
TheAvengerCommented:
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
 
Yann de ChamplainAssociateAuthor Commented:
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
 
TheAvengerCommented:
Probably, yes :-(( I didn't know that
0
 
Yann de ChamplainAssociateAuthor Commented:
I would like to give points to both of you.
I am not sure how to achieve that.
Do you know how?
0
 
TheAvengerCommented:
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

Industry Leaders: 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!

  • 7
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now