Add Columns to a datagrid from a 2nd datatable (unbound)

A

A. Burch

I have 2 data sources (1 - DB, 2 - unbound table). I want to be able to
display all of the columns in a datagrid on a form. I can't seem to make
the unbound table show up when I run the program. The data table for the
2nd unbound table starts with nothing in it but has 5 columns defined. I
would like to see all of the columns from the DB and unbound table when the
form loads instead I just get the DB column names and data from the DB. My
data sources seem to be working correctly but I'm not able to get the
DataGrid to work correctly. I use a button on the form to start/stop the
async. data source and it does work, but I can't get the data to show up in
the DataGrid. I have tried MissingMappingAction, MissingSchemaAction and
doing a merge with the 2nd datatable. I decided to recode it and use an add
but that isn't working either?

What should I be doing to make to get the results I need.


c# code for this....

da = new OleDbDataAdapter(strSQL, oCon);
ds = new DataSet();
oCon.Open();
dt = new DataTable("unbound");
dc = new DataColumn();
// Last
dc.AutoIncrement = false;
dc.Caption = "Last";
dc.ReadOnly = false;
dc.Unique = false;
dt.Columns.Add("Last", typeof(Double));
// Bid
dc = new DataColumn();
dc.AutoIncrement = false;
dc.Caption = "Bid";
dc.ReadOnly = false;
dc.Unique = false;
dt.Columns.Add("Bid", typeof(Double));
// Ask
dc = new DataColumn();
dc.AutoIncrement = false;
dc.Caption = "Ask";
dc.ReadOnly = false;
dc.Unique = false;
dt.Columns.Add("Ask", typeof(Double));
// Volume
dc = new DataColumn();
dc.AutoIncrement = false;
dc.Caption = "Volume";
dc.ReadOnly = false;
dc.Unique = false;
dt.Columns.Add("Volume", typeof(Int32));
// Momentum
dc = new DataColumn();
dc.AutoIncrement = false;
dc.Caption = "Momentum";
dc.ReadOnly = false;
dc.Unique = false;
dt.Columns.Add("Momentum", typeof(Int32));
ds.Tables.Add(dt);
//da.MissingMappingAction = MissingMappingAction.Ignore;
//da.MissingSchemaAction = MissingSchemaAction.Ignore;
numrows = da.Fill(ds);
//ds.Merge(dt,false,System.Data.MissingSchemaAction.Add);

Thanks....
Al
 
M

Miha Markic

Hi,

Your problem lies in table mapping probably - you can check it by examining
the dataset after Fill - you'll see that it has two tables.
Your table name is "unbound" while adapter fills into "tablename in
database" datatable if not otherwise specified.
You have several possibilites at this point:
- adjust table name
- use ds.Fill(dt)
- create correct mapping

First two are the simpliest ones.
 
A

A. Burch

I got it to work correctly by using a data table instead of the dataset
and filling a datatable instead of a dataset. Thanks Miha,
code now is like this:

da = new OleDbDataAdapter(strSQL, oCon);
ds = new DataSet();
oCon.Open();
dt = new DataTable("unbound");
da.Fill(dt);
dc = new DataColumn();
// Last
dc.AutoIncrement = false;
dc.Caption = "Last";
dc.ReadOnly = false;
dc.Unique = false;
dt.Columns.Add("Last", typeof(Double));
// Bid
dc = new DataColumn();
dc.AutoIncrement = false;
dc.Caption = "Bid";
dc.ReadOnly = false;
dc.Unique = false;
dt.Columns.Add("Bid", typeof(Double));
// Ask
dc = new DataColumn();
dc.AutoIncrement = false;
dc.Caption = "Ask";
dc.ReadOnly = false;
dc.Unique = false;
dt.Columns.Add("Ask", typeof(Double));
// Volume
dc = new DataColumn();
dc.AutoIncrement = false;
dc.Caption = "Volume";
dc.ReadOnly = false;
dc.Unique = false;
dt.Columns.Add("Volume", typeof(Int32));
// Momentum
dc = new DataColumn();
dc.AutoIncrement = false;
dc.Caption = "Momentum";
dc.ReadOnly = false;
dc.Unique = false;
dt.Columns.Add("Momentum", typeof(Int32));
numrows = da.Fill(dt);
}
catch
{
throw;
}
return dt;
 

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