How to debug DataGridTableStyle?

B

Brett Romero

When I create this table and add it to my DataGrid (framework v1.1),
the TableStyle works fine:

DataTable dt = new DataTable();
DataColumn dc = new DataColumn("AreaCode");
DataColumn dctwo = new DataColumn("Test");
dt.Columns.Add(dc);
dt.Columns.Add(dctwo);

I have the style set to only show AreaCode. However, when I use a
table from the database, every column displays. AreaCode is a column
in this table, which means I should only see AreaCode in the grid.

I've looked at the grid properties in the debugger and see the
tablestyle AreaCode column is there. I also see in the DataTable a
column named AreaCode. What else do I need to look for to figure it
out?

Thanks,
Brett
 
N

Nicholas Paldino [.NET/C# MVP]

Brett,

The problem here isn't the grid and how it is set up, but rather, the
table itself. You say when you get a database from the table, this happens.
Well, are you selecting all of the fields, or some of them? If it is all of
them, then that's what you are going to see in the grid.

You need to cut down your selection so that you only select the fields
that you want.

Hope this helps.
 
B

Brett Romero

That's not completely correct Nick. I'm doing this with a custom
datagrid. On the regular winform datagrid, I use a tablestyle to
dispay 8 of 10 fields returned in the set from a database.

This has to be something specific to my custom grid. It's puzzling
though b/c the little two column table I created works with the custom
grid. I can display 1 of 2 columns. How is that any different than
what comes from the database and is put into a dataset, which I then
reference?

It has more column than I want displayed. However, the tablestyle
takes care of that. Just not yet because I'm missing something in my
custom grid's logic, which I'm trying to pin point.

Thanks,
Brett
 
G

Guest

Brett,
Try using MappingType.Hidden to suppress display of columns you don't want
displayed, e.g.:
ds.Tables[2].Columns["DeviceTypeModelID"].ColumnMapping = MappingType.Hidden;

Peter
 
N

Nicholas Paldino [.NET/C# MVP]

Brett,

Well, post a code sample and let's see for ourselves.

DataSets that are populated from a DataAdapter tend to be no different
than anything populated by hand. However, that doesn't mean that when you
populate something by hand, you might miss what a DataAdapter might normally
set.
 
B

Brett Romero

I use the MS DataAccess block, which uses an sqlhelper. It returns a
data reader, which I loop through to create a dataset. So there isn't
a data adapter involved in the code. What else can I post?

Thanks,
Brett
 
B

Bart Mermuys

Hi,

Brett Romero said:
When I create this table and add it to my DataGrid (framework v1.1),
the TableStyle works fine:

DataTable dt = new DataTable();
DataColumn dc = new DataColumn("AreaCode");
DataColumn dctwo = new DataColumn("Test");
dt.Columns.Add(dc);
dt.Columns.Add(dctwo);

I have the style set to only show AreaCode. However, when I use a
table from the database, every column displays. AreaCode is a column
in this table, which means I should only see AreaCode in the grid.

I've looked at the grid properties in the debugger and see the
tablestyle AreaCode column is there. I also see in the DataTable a
column named AreaCode. What else do I need to look for to figure it
out?

The first thing to check when DGColumnStyle's don't seem to work is whether
you have set the correct MappingName for the DGTableStyle,
DGTableStyle.MappingName must be the same as the bound DataTable.TableName.

DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = someDataTable.TableName; // !!

.... add DataGridColumnStyles ...

someDataGrid.TableStyles.Add( dgts );
someDataGrid.DataSource = someDataTable;


HTH,
Greetings
 
N

Nicholas Paldino [.NET/C# MVP]

Well, SOME code would have been helpful, but I digress.

Why are you populating the data set yourself? You should be able to
tell the data access block to populate the data set for you, using a data
adapter.
 
B

Brett Romero

Bart, you absolutely got it! That was the disconnect. The table name
and tablestyle mappingname were different.

I'd like to override DataSource() in my custom grid so I can look for
the table name. This way I can be sure the mapping and table names are
always the same. However, the datasource may take a dataset, datatable
or dataview. I'd need to know how to get at the table to retreive the
table name. Do I need to setup switch statements in the DataSource()
method, look for each type and extract the table name?

Thanks,
Brett
 
B

Bart Mermuys

Hi,

Brett Romero said:
Bart, you absolutely got it! That was the disconnect. The table name
and tablestyle mappingname were different.

I'd like to override DataSource() in my custom grid so I can look for
the table name. This way I can be sure the mapping and table names are
always the same. However, the datasource may take a dataset, datatable
or dataview. I'd need to know how to get at the table to retreive the
table name. Do I need to setup switch statements in the DataSource()
method, look for each type and extract the table name?

A little advanced, but you could use a CurrencyManager for this.
CurrencyManager.List points to the list that is actually bound, that list
will implement ITypedList if it has a name otherwise the type-name must be
used, example:

// DataMember can be an empty string (eg. when bound to DataTable)
CurrencyManager cm = (CurrencyManager)
BindingContext[DataSource, DataMember];

ITypedList typedList = cm.List as ITypedList;
string listName = "";
if ( typedList!=null )
listName = typedList.GetListName(null);
else
listName = cm.List.GetType().Name;

That should work for almost anything (DataSet, DataTable, DataView, custom
lists, arrays ).

Offcourse you could also use a switch but then it's less generic.

HTH,
Greetings
 

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