如何构建具有未知列数的WPF Datagrid [英] How to build A WPF Datagrid with an unknown number of columns

查看:69
本文介绍了如何构建具有未知列数的WPF Datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我从txt获取的字符串数组集合中构建并显示WPF数据网格。问题是我不知道先验的列数,即单个数组中的项目数。所以我在我的xaml < DataGrid Grid.Row = 2 ItemsSource = {Binding Path = Rows} />


中定义

我试图将其填充到我的视图模型中,但是我不能简单地将数组的集合(Observable Collection)作为项目源,因为数据网格将仅显示空白行。



我也可以对Observable集合使用其他方法,因为我以相同的方法生成数组



这是我的Observable集合:

  ObservableCollection< string []>行=新的ObservableCollection< string []> ;; 

我用这种方法填充集合

  foreach(wsettings.lista中的ListViewItem项)
{
TextBlock line = item.Content as TextBlock;
字符串txt = line.Text;
string [] x = txt.Split(stringSeparators,StringSplitOptions.None);
Rows.Add(x);
}

请不要介意分割前的第一部分。我从以前使用的文本块的列表视图中获取数据(我有理由)。



EDIT1:使代码更具可读性



EDIT2:标头必须是用户必须设置的组合框

解决方案


I need to build and display a WPF data grid from a collection of string array that i got from a txt. The problem is that i don't know a priori which will be the number of columns i.e. the number of item in the single array. So i defined in my xaml <DataGrid Grid.Row="2" ItemsSource="{Binding Path=Rows}" />

I was trying to fill it in my View Model, but i cannot simply put my collection (Observable Collection) of array as item source, since the datagrid will display only blank rows.

I can also use other approach over the Observable collection since i produce my array in the same method

this is my Observable Collection:

ObservableCollection<string[]> Rows = new ObservableCollection<string[]>;

in this method i fill the collection

foreach(ListViewItem item in wsettings.lista)
        {                 
            TextBlock line = item.Content as TextBlock;
            string txt = line.Text;
            string[] x = txt.Split(stringSeparators, StringSplitOptions.None);               
            Rows.Add(x);
        }    

Please don't mind the first part before the split. I take my data from a listview of text block that i used before(I have my reason).

EDIT1: made the code more readable

EDIT2: the header must be a combobox that a user must set

解决方案

There is DataTable class in .Net. Its primary purpose is to communicate with relational database but it can be used nicely to store, display and edit tabular data (e.g. read and display .csv/Excel files -> DataTable + DataGrid in wpf, DataTable + DataGridView in WinForms).

DataTable columns (DataColumn) can be added/removed at runtime and DataGrid auto-generates columns (DataGridColumn) for them (enabled by default), using Name property for headers. Also DataTable supports sorting and filtering out-of-box.

note: DataGrid doesn't clear Columns when new ItemsSource is assigned. So it is possible to have some predefined columns and use autogenerate as well.

note: DataGrid creates DataGridTextColumns by default. If more complex template is required, the process can be intercepted via AutoGeneratingColumn event (see example)

here is an example:

code

public class MyViewModel
{
    public DataTable Test { get; set; }
}

public MyWindow()
{
    InitializeComponent();
    var vm = new MyViewModel
                {
                    Test = new DataTable
                        {
                            Columns = {"A", "B", "C"}
                        }
                };            
    this.DataContext = vm;

}

xaml

<DataGrid AutoGenerateColumns="True"
          ItemsSource="{Binding Path=Test.DefaultView}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="#"/>
    </DataGrid.Columns>
</DataGrid>

the result (I entered some values)

这篇关于如何构建具有未知列数的WPF Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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