添加行一个WPF数据网格中列被直到运行时才知道 [英] Adding rows to a WPF datagrid where columns are not known until runtime

查看:114
本文介绍了添加行一个WPF数据网格中列被直到运行时才知道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据添加到一个DataGrid(事实上,在网格中会做介绍数据的任何控制),但列(包括姓名和号码)不知道,直到运行时。



列我知道了如何创建:如

  DataGridTextColumn textColumn =新DataGridTextColumn() ; 
textColumn.Header = column.DisplayName;
MyDataGrid.Columns.Add(textColumn);



但我怎么添加行?我没有看到,因为我的数据不会在已知属性的对象包含我如何使用绑定。例如,对于每行数据可能进来作为字符串[]。所以有一次我可能有三列,其他时间我可能有五个



我期待着做能够做这样的事:

  //例的数据来代表一行。 
的String [] = ROW1新[] {值1,值2,值3};

无功行=新行;
row.AddCell(ROW1 [0]);
row.AddCell(ROW1 [1]);
row.AddCell(ROW1 [2]);
MyDataGrid.Rows.Add(行);


解决方案

我不得不开始堵VS客场让我的头一轮确切的代码,但你可以很可能只是创建列和使用列键绑定表达式看到,因为索引绑定在WPF工作



我会得到在一分钟内一些代码了 - 但它会看起来像你的新行生成代码,但与上看起来像(原谅可能是不正确的方法名)



的列绑定

  textColumn.Bindings.Add(新绑定(本[+ columnIndex.ToString()+])); 



更新:



呀不知道这是你在找什么,但它的工作原理:



创建一个单一的窗口,它(DataGrid1中)



一个DataGrid

 公共主窗口()
{
的InitializeComponent();

变种山坳=新DataGridTextColumn();
col.Header =列1;
col.Binding =新结合([0]);
dataGrid1.Columns.Add(COL);

列=新DataGridTextColumn();
col.Header =列2;
col.Binding =新结合([1]);
dataGrid1.Columns.Add(COL);

列=新DataGridTextColumn();
col.Header =栏3;
col.Binding =新结合([2]);
dataGrid1.Columns.Add(COL);

//dataGrid1.ad

名单,LT;对象>行=新的List<对象>();
的String []值;

值=新的字符串[3];

值[0] =你好;
值[1] =世界;
值[2] =底;
rows.Add(值);

dataGrid1.ItemsSource =行;
}




I'm trying to add data to a datagrid (in fact, any control that presents data in a grid will do), but the columns (both names and numbers) are not known until runtime.

The columns I DO know how to create: E.g

DataGridTextColumn textColumn = new DataGridTextColumn();
textColumn.Header = column.DisplayName;
MyDataGrid.Columns.Add(textColumn);

But how do I add rows? I don't see how I can use binding because my data is not contained in an object with known properties. For example, data for each row might come in as a string[]. So one time I might have three columns, another time I might have five.

I was expecting do be able to do something like this:

// Example data to represent a single row.
string[] row1 = new[] { "value1", "value2", "value3" };

var row = new Row;
row.AddCell(row1[0]);
row.AddCell(row1[1]);
row.AddCell(row1[2]);
MyDataGrid.Rows.Add(row);

解决方案

I'd have to start plugging away in VS to get my head round the exact code, but you can most likely just create your columns and use the column key as the binding expression seeing as indexed bindings work in WPF

I'll get some code up in a minute - but it would look something like your row creation code but with bindings on the columns that look something like (forgive the possibly incorrect method names)

textColumn.Bindings.Add(new Binding("this[" + columnIndex.ToString() + "]"));

Update:

Yeah not sure if this is what you are looking for but it works:

Created a single window with a datagrid on it (dataGrid1)

 public MainWindow()
    {
        InitializeComponent();

        var col = new DataGridTextColumn();
        col.Header = "Column1";
        col.Binding = new Binding("[0]");
        dataGrid1.Columns.Add(col);

        col = new DataGridTextColumn();
        col.Header = "Column2";
        col.Binding = new Binding("[1]");
        dataGrid1.Columns.Add(col);

        col = new DataGridTextColumn();
        col.Header = "Column3";
        col.Binding = new Binding("[2]");
        dataGrid1.Columns.Add(col);

        //dataGrid1.ad

        List<object> rows = new List<object>();
        string[] value;

        value = new string[3];

        value[0] = "hello";
        value[1] = "world";
        value[2] = "the end";
        rows.Add(value);

        dataGrid1.ItemsSource = rows;
    }

这篇关于添加行一个WPF数据网格中列被直到运行时才知道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆