DataGridTableStyle not being applied

R

Rich

I have working sample code demonstrating the use of DataGridTableStyle,
but for some reason it's not working for me. The column headings just
assume the field names instead of the "HeaderText" I specify and any
other properties I set are ignored. Any ideas?

Code:

SqlCeConnection cn = new SqlCeConnection(@"Data Source=\My
Documents\Databases\EquipCert.sdf");

string sql = "select entry_id, step_name from tblCertData ";
SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(sql, cn);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
dgData.DataSource = dataSet.Tables[0];

DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = "tblCertData";

DataGridColumnStyle entryId = new DataGridTextBoxColumn();
entryId.MappingName = "entry_id";
entryId.HeaderText = "";
entryId.Width = 0;
ts.GridColumnStyles.Add(entryId);

DataGridColumnStyle step = new DataGridTextBoxColumn();
step.MappingName = "step_name";
step.HeaderText = "Step";
step.Width = 50;
ts.GridColumnStyles.Add(step);

dgData.TableStyles.Add(ts);


Thank you.
 
P

Peter Foot [MVP]

Check that the MappingName exactly matches your table name e.g.
ts.MappingName = dataSet.Tables[0].TableName;

Peter
 
R

Rich

Since the data source for the data grid is set in another class (I
simplified things for this posting) I don't have access to the dataSet
in the manner you describe below:

ts.MappingName = dataSet.Tables[0].TableName;

I have, however, verified that the value for ts.MappingName is exactly
the same as the table I'm querying. Should this matter?

Also, if my query included joins to multiple tables (which this one does
not), how would I accomodate that?

Thanks again.
 
P

Peter Foot [MVP]

The name of the DataTable in your dataset may not exactly match the
tablename in your database, in fact I think the default is "Table" or
something similar. You could approach this another way and examine the
object assigned to the DataSource property e.g.

DataTable dt = (DataTable)dgData.DataSource;
ts.MappingName = dt.TableName;

You would probably want some error checking around this to make sure the
datasource is actually a datatable object. Another option would be to
specify the tablename when you fill the dataset e.g.
dataAdapter.Fill(dataSet, "MyDataTable");
then you can use
ts.MappingName = "MyDataTable";

Peter
 
R

Rich

Using your code below, the value returned for "dt.TableName" is "Table."
I have no idea where that's coming from. No errors result, but I also
don't get any of the column style formatting I specify.

Weird stuff. I'm probably doing something very stupid that's causing
problems, but I'll be darned if I can figure it out.
 
I

Ilya Tumanov [MS]

Actually, it is "Table" (no dot). That's the default name DataAdapter
assigns to the table.
It works the same way on desktop (with SQL Client) as well. Here's the
quote from documentation (look for DataAdapter class):

Fill
Supported by the .NET Compact Framework.
Adds or refreshes rows in the DataSet to match those in the data source
using the DataSet name, and creates a DataTable named "Table".

You can change the table name to whatever name you need.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 

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