Filling a Datagrid with dynamic Columns

后端 未结 4 1189
清歌不尽
清歌不尽 2020-12-12 20:40

I have an Datagrid which needs to get filled dynamicly.

The tablelayout is like:

id | image | name | Description | Name-1 | Name-N

4条回答
  •  旧时难觅i
    2020-12-12 21:00

    2nd approach: use a DataTable. This makes use of the custom-type infrastructure under the hood, but is easier to use. Just bind the DataGrid's ItemsSource to a DataTable.DefaultView property:

    This almost worked but instead of binding to the DataTable.DefaultView property property I created a property of type DataView and bound to that.

    
    

    This allows the binding to be two way, binding to the DataTable.DefaultView cannot be a TwoWay binding. In the View Model

        public DataView DataView
        {
            get { return _dataView; }
            set
            {
                _dataView = value;
                OnPropertyChanged("DataView");
            }
        }
    

    With this setup I could not only define the columns dynamically when the View Model is initialized, but could update and change the data table dynamically at any time. In using the approach as defined by McGarnagle above, the view schema was not refreshing when the DataTable was updated with a new data source.

提交回复
热议问题