1
EditVariationWindowModel edit = (EditVariationWindowModel)this.DataContext;

        var datagrid = dataGrid3;



        foreach (Variation variation in edit.SelQuestion.Variations)
        {
            foreach (var parameter in variation.QuestionParameters)
            {
                var binding = new Binding(parameter.Value);
                var column = new DataGridTextColumn();
                column.Header = parameter.Key.Name;
                column.Binding = binding;
                datagrid.Columns.Add(column);

            }
        }

so this is my code in the code behind for my datagrid. I work in wpf.

Now what is the problem: I just get one Row but many duplicate column headers(with the right bindings) but as you will understand already, I don't want them next to eachother but under eachother. for example:

not like this

header1 | header2 | header1 | header2|
string1 | string2 | string 1| string2|

but

 header1 | header2
 string1 | string2
 string1 | string2

Anyone knows how I can solve this?

2
  • Does Variation var in edit.SelQuestion.Variations even compile? I thought var was a reserved word in C#? And even if it is "legal" I wouldn't use it, because it's confusing, I'd rename it variation. Commented May 3, 2011 at 7:50
  • 1
    yeah it compiles... but ok, I will change it Commented May 3, 2011 at 7:53

2 Answers 2

0

You have to add first all the columns, and then select them when adding data.

What you're doing is to add a binding with a new column.

EDIT:

What I usually do when I have to Add Rows manually is:

1.- Add DataColumn to DataTable and ColumnStyle to the DataGrid just like:

DataColumn fNameColumn8 = new DataColumn();
fNameColumn8.DataType = System.Type.GetType("System.String");
m_dataTable.Columns.Add(fNameColumn8);

ColumnStyle myStyleColumn8 = new ColumnStyle(7);
myStyleColumn8.TextAlign = ContentAlignment.TopRight;

DataGridTableStyle dataGridTableStyle = new DataGridTableStyle();
dataGridTableStyle.MappingName = MAPPINGNAME;
dataGridTableStyle.GridColumnStyles.Add(myStyleColumn8);

this.dataGrid.TableStyles.Add(dataGridTableStyle);

2.- Assign mapping name and name to show to ColumnStyles of dataGrid and Columns of DataTable:

m_dataTable.Columns[8].ColumnName = this.m_strHeader;

((DataGridTextBoxColumn)this.dataGrid.TableStyles[0].GridColumnStyles[8]).MappingName = this.m_strHeader;

((DataGridTextBoxColumn)this.dataGrid.TableStyles[0].GridColumnStyles[8]).HeaderText = this.m_strHeader;

3.- Assign the width of the column in the ColumnStyles of the DataGrid:

((DataGridTextBoxColumn)this.dataGrid.TableStyles[0].GridColumnStyles[8]).Width = 20;

4.- Fill the rows:

DataRow dataRow = this.m_dataTable.NewRow();

dataRow[this.m_strHeader] = "DATA";

this.m_dataTable.Rows.Add(dataRow);
Sign up to request clarification or add additional context in comments.

4 Comments

But how do you do that? I know how to make my columns.. But I don't know how to add a new row foreach questionparameter
Using DataTable as the DataGrid.DataSource, you can do ((DataTable)dataGrid.DataSource).NewRow() that gives to you a new DataRow that you can Add to the DataSourceDirectly, that's what you have to do.
and how do I implement this? so first I make my columns? and what do I have to do then?
I posted an individual example, you have to init each column before of inserting rows.
0

You're adding a column definition for each row... WTF?

Here's a decent tutorial on How to Bind a DataGrid to a Collection.

Cheers. Keith.


EDIT:

Try

foreach (var parameter in edit.SelQuestion.Variations.First().QuestionParameters)

to define your grid columns... see: First method

Then (as a seperate step) populate the datagrid by iterating through the Variations... or better still READ the article linked above, and just bind the grid to the collection. No need to mess-about defining columns, and looping through each row... The grid can do ALL that automatically.

1 Comment

But here I have the problem that I can't set my ItemsSource, because I need the list questionparameters of each variation... so when i set my itemssource to SelQuestion.Variations, I can't reach my property: Questionparameters of Variation. Or is there a way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.