newbie question

F

Frederik

Hi all,

I've been working a little with data binding in ASP.NET, but know I
have to make a console application using C#. The way of filling a
DataGrid seems a bit different. I can't see a DataBind() method for
the DataGrid object. What I have is the following code:

/ * start code snippet */
string connectionString = "...";
OleDbConnection dbConnection = new OleDbConnection(connectionString);
OleDbCommand OleCmd = new OleDbCommand("SELECT * FROM sol_history
ORDER BY Verstuurd", dbConnection);
OleDbDataAdapter DbAdapter = new OleDbDataAdapter();
DataSet DbDataSet = new DataSet();
DbAdapter.SelectCommand = OleCmd;
DbAdapter.Fill(DbDataSet);
DGridJobs.SetDataBinding(DbDataSet, "");
/ * end code snippet */

The code fills the DataGrid in a 'weird' kind of way: it seems to have
something like a parent table. Could someone tell me what I need to do
in order to fill the DataGrid in a 'normal' way (with normal I mean
without the parent table stuff)?

Your time and answers are appreciated,
Fre
 
F

Frederik

Hi Jay,

Thanks for your input, but now the DataGrid is empty and there's no way to
navigate to my 'child table' (seems logical looking at the name of the
property). The thing is, that I only want to see the requested data in the
DataGrid, like it works in asp.net.

Regards,
Frederik
 
V

Vagabond Software

Frederik said:
/ * start code snippet */
string connectionString = "...";
OleDbConnection dbConnection = new OleDbConnection(connectionString);
OleDbCommand OleCmd = new OleDbCommand("SELECT * FROM sol_history
ORDER BY Verstuurd", dbConnection);
OleDbDataAdapter DbAdapter = new OleDbDataAdapter();
DataSet DbDataSet = new DataSet();
DbAdapter.SelectCommand = OleCmd;
DbAdapter.Fill(DbDataSet);
DGridJobs.SetDataBinding(DbDataSet, "");
/ * end code snippet */

The code fills the DataGrid in a 'weird' kind of way: it seems to have
something like a parent table. Could someone tell me what I need to do
in order to fill the DataGrid in a 'normal' way (with normal I mean
without the parent table stuff)?

Use the DataMember property of the DataGrid.. For Example:

DGridJobs.DataMember = DbDataSet.Tables[0];

If the compiler complains about that line of code, the try this:

DGridJobs.DataMember = DbDataSet.Tables[0].TableName;

Have fun...

- carl
 
F

Frederik

Hi carl,

Thanks for your answer. Your second suggestion did compile, but it gave me
the same result as before:

http://users.skynet.be/am044448/Uitwisseling/DGridJobs1.png
http://users.skynet.be/am044448/Uitwisseling/DGridJobs2.png

It's not a problem for me to expand the DataSet till I get the wanted
result, but I'm not sure if the users of my program will find it as easy as
I do.

Regards,
Frederik


Vagabond Software said:
/ * start code snippet */
string connectionString = "...";
OleDbConnection dbConnection = new OleDbConnection(connectionString);
OleDbCommand OleCmd = new OleDbCommand("SELECT * FROM sol_history
ORDER BY Verstuurd", dbConnection);
OleDbDataAdapter DbAdapter = new OleDbDataAdapter();
DataSet DbDataSet = new DataSet();
DbAdapter.SelectCommand = OleCmd;
DbAdapter.Fill(DbDataSet);
DGridJobs.SetDataBinding(DbDataSet, "");
/ * end code snippet */
The code fills the DataGrid in a 'weird' kind of way: it seems to have
something like a parent table. Could someone tell me what I need to do
in order to fill the DataGrid in a 'normal' way (with normal I mean
without the parent table stuff)?

Use the DataMember property of the DataGrid.. For Example:
DGridJobs.DataMember = DbDataSet.Tables[0];
If the compiler complains about that line of code, the try this:
DGridJobs.DataMember = DbDataSet.Tables[0].TableName;
Have fun...
- carl
 
V

Vagabond Software

Frederik said:
Hi carl,

Thanks for your answer. Your second suggestion did compile, but it gave me
the same result as before

Setting the DataMember property should present the table you desire, as it does in this code block:

private void FillDataGrid()
{
string sqlSelect = "SELECT * FROM Customers";
sqlconn = new SqlConnection(ConnectionString);
sqlAdapter = new SqlDataAdapter(sqlSelect, sqlconn);

try
{
sqlAdapter.Fill(ds, "Customers");
}
finally
{
sqlconn.Close();
}

dataGrid1.DataSource = ds;
dataGrid1.DataMember = ds.Tables["Customers"].TableName;
}

If setting the DataMember property AFTER setting the DataSource property doesn't display the specified table, then try assigning the table to the DataSource property.

- carl
 
F

Frederik

Thanks a lot Carl, it works fine now !!! [I made a mistake first time.]

Frederik

--
--


Setting the DataMember property should present the table you desire, as it
does in this code block:

private void FillDataGrid()
{
string sqlSelect = "SELECT * FROM Customers";
sqlconn = new SqlConnection(ConnectionString);
sqlAdapter = new SqlDataAdapter(sqlSelect, sqlconn);

try
{
sqlAdapter.Fill(ds, "Customers");
}
finally
{
sqlconn.Close();
}

dataGrid1.DataSource = ds;
dataGrid1.DataMember = ds.Tables["Customers"].TableName;
}

If setting the DataMember property AFTER setting the DataSource property
doesn't display the specified table, then try assigning the table to the
DataSource property.

- carl
 

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