简体   繁体   English

C# 数据网格绑定问题

[英]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;
                datagrid.Columns.Add(column);

            }
        }

so this is my code in the code behind for my datagrid.所以这是我的数据网格背后的代码中的代码。 I work in wpf.我在 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.现在问题出在哪里:我只得到一个 Row 但许多重复的列标题(具有正确的绑定),但正如您已经理解的那样,我不希望它们彼此相邻,而是在彼此下方。 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're adding a column definition for each row... WTF?您正在为每一行添加一个列定义...... WTF?

Here's a decent tutorial on How to Bind a DataGrid to a Collection .这是一个关于如何将 DataGrid 绑定到 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.无需纠结于定义列和循环遍历每一行......网格可以自动完成所有这些。

You have to add first all the columns, and then select them when adding data.您必须先添加所有列,然后在添加数据时添加 select 它们。

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: 1.- 将 DataColumn 添加到 DataTable 并将 ColumnStyle 添加到 DataGrid 就像:

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: 2.- 将映射名称和名称分配给 dataGrid 的 ColumnStyles 和 DataTable 的 Columns:

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: 3.- 在 DataGrid 的 ColumnStyles 中指定列的宽度:

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

4.- Fill the rows: 4.- 填写行:

DataRow dataRow = this.m_dataTable.NewRow();

dataRow[this.m_strHeader] = "DATA";

this.m_dataTable.Rows.Add(dataRow);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM