开发者

C# datagrid problem with binding

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;
 开发者_StackOverflow中文版               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?


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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜