简体   繁体   中英

C# WPF How to fill my DataGrid programmatically

I ve just started learning WPF, and trying to set the DataGrid with some dataset. I have the appointments from Outlook, and its week numbers. in lst I have list of the appointments that have properties, one of them is "cwHours" - calendar week hours - float array, indexes of which corresponds to calendar week numbers. And the values inside are working hours. So that's the problem screenshot As seen on the screenshot, my array of cwhours is displayed in the column as Single array, But it should be splitted in columns by its indexes in the right side of project name. How can I put the array into the DataGrid, not in one column but in different ones? Thank you.

        List<ProjectModel> lst = reader.olProjectList;
        DataTable.ItemsSource = lst;

        foreach(var x in lst)
        {
            float[] arrayofcw = x.cwHours;

            for (int i = 0; i < arrayofcw.Length; i++)
            {
                var col = new DataGridTextColumn();

                col.Header = i;
                DataTable.Columns.Add(col);
            }





        }

You should set the Binding property of the column. You could set the Path of the Binding to an index in the array property, eg:

var col = new DataGridTextColumn();
col.Binding = new Binding("cwHours[" + i + "]");
col.Header = i;
DataTable.Columns.Add(col);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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