简体   繁体   中英

Display dynamic columns in WPF DataGrid from Datatable using MVVM

I'm using MVVM pattern to develop a WPF application. I want to display dynamic columns to DataGrid from Datatable.

I have added code to display it but it is not working. Datatable is filled with data but In datagrid , data is not displaying.

XAML:

 <DataGrid Grid.Row="1" AutoGenerateColumns="False" GridLinesVisibility="All" Foreground="Black" ItemsSource="{Binding ItemSource, Mode=OneWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"  IsReadOnly="True" />

ViewModel:

private DataView _itemSource;

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

public async Task PopulateData(string queryText)
{
    var dt = await CustomReportQueryDAO.GetCustomReportQueryResult("select * from [Person]");
    ItemSource = dt.DefaultView;
}         

You have set AutoGenerateColumns="False" on the DataGrid and you have not defined any columns.

Try changing to AutoGenerateColumns="True"

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