Why doesn't DataGridTableStyle work?

B

Brett Romero

I can't find what exactly I'm doing wrong that the following
DataGridTableStyle is not working on my table. The result set returns
21 columns. I'm only formatting 5 via the TableStyle. I thought maybe
the problem was I had more columns being put into the DataGrid than I
was formatting. I then created a new table and adding only the five
mapped columns to it. That didn't help either.

I'm hiding column 3 because I need its value but don't want it
displayed. I do get the five columns output into the grid but no
styling. Any suggestions:

DataTable dt = new DataTable();

DataGridTableStyle participantGridTableStyle = new
DataGridTableStyle();
participantGridTableStyle.MappingName = dt.TableName;

// Create GridColumnStyle objects for the grid columns
//First five columns are: PersonId, PersonCode, FirstName,
MiddleName, LastName

DataGridTextBoxColumn partCol1 = new DataGridTextBoxColumn();
DataGridTextBoxColumn partCol2 = new DataGridTextBoxColumn();
DataGridTextBoxColumn partCol3 = new DataGridTextBoxColumn();
DataGridTextBoxColumn partCol4 = new DataGridTextBoxColumn();
DataGridTextBoxColumn partCol5 = new DataGridTextBoxColumn();

partCol1.MappingName = "PersonId";
partCol2.MappingName = "PersonCode";

//hide column 3
partCol3.MappingName = "FirstName";
partCol3.Width = 0;

partCol4.MappingName = "MiddleName";

partCol5.MappingName = "LastName";
//just to see if style is outputting
partCol5.HeaderText = "Last | Name";
partCol5.
partCol5.Width = 65;
partCol5.Alignment = HorizontalAlignment.Left;
partCol5.TextBox.Enabled = false;

// Add the GridColumnStyles to the DataGrid's Column Styles
collection.
// Place the "ID" column (column 1) last since it is not visible.

participantGridTableStyle.GridColumnStyles.Add(partCol1);
participantGridTableStyle.GridColumnStyles.Add(partCol2);
participantGridTableStyle.GridColumnStyles.Add(partCol3);
participantGridTableStyle.GridColumnStyles.Add(partCol4);
participantGridTableStyle.GridColumnStyles.Add(partCol5);



dt = (Searching.FindParticipants()).Tables[0];

//dt.TableName will now be "Table" because of above assignement.
dt.TableName = "ParticipantSearch";

DataTable dtLimit = dt.Copy();;


//restrict to only five columns
int x = 5;
while (x < dtLimit.Columns.Count)
{
dtLimit.Columns.RemoveAt(x);
Debug.WriteLine(dt.Columns[x].ColumnName);
}

Participant.TableStyles.Clear();
Participant.TableStyles.Add(participantGridTableStyle);

this.Participant.DataSource = dtLimit;
Participant.Expand(-1);
Participant.NavigateTo(0, dt.TableName);

Thanks,
Brett
 
S

Steven Nagy

Well I am not sure if this is it or not but....

You have this line:

participantGridTableStyle.MappingName = dt.TableName;

Then later on you have this line:

dt.TableName = "ParticipantSearch";

Which means your DGTS is mapping to a table that doesn't really exist.

Try putting the MappingName assignment lower down.

Steven Nagy
 
B

Brett Romero

Glad you posted back to this Steven. You are right. The problem was a
combination of what you mentioned and previously this line resetting
the table name.

dt = (Searching.FindParticipants()).Tables[0];

It reset the dt table back to "Table". That's why this line now
follows it.

dt.TableName = "ParticipantSearch";

Thanks,
Brett
 

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