Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

c# - How to build A WPF Datagrid with an unknown number of columns

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

datagrid


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...