DataTable绑定到DataGrid WPF

时间:2015-03-27 01:54:28

标签: c# wpf xaml datagrid

XAML文件:

<DataGrid ItemsSource="{Binding Items}" x:Name="ItemsDataGrid" Margin="0,30,0,0" AutoGenerateColumns="True">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Sl. No." MinWidth="125" />
        <DataGridTextColumn Header="Name" MinWidth="200" />
        <DataGridTextColumn Header="Total Quantity" MinWidth="200" />
        <DataGridTextColumn Header="Remaining" MinWidth="200" />
        <DataGridTextColumn Header="Cost/Each" MinWidth="200" />
    </DataGrid.Columns>
</DataGrid>

XAML.CS文件:

private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
    _itemsViewModel = new ItemsViewModel();
    this.DataContext = _itemsViewModel;
}

ViewModel.cs文件:

private DataTable _itemsList;
private List<ItemsModel> ItemsModel;

public DataTable ItemsList
{
    get { return _itemsList; }
    set
    {
        _itemsList = value;
        OnPropertyChanged("ItemsList");
    }
}

private List<ItemsModel> _items;

public List<ItemsModel> Items
{
    get { return _items; }
    set
    {
        _items = value;
        OnPropertyChanged("Items");
    }
}

public ItemsViewModel()
{
    LoadItems();
}

private void LoadItems()
{
    CFunctions cFunctions = new CFunctions();
    ItemsList = cFunctions.GetItems();
    ItemsModel = new List<ItemsModel>();
    for (int i = 0; i < ItemsList.Rows.Count; i++)
    {
        ItemsModel.Add(new ItemsModel()
        {
            SlNo = int.Parse(ItemsList.Rows[i].ItemArray[0].ToString()),
            ItemName = ItemsList.Rows[i].ItemArray[1].ToString(),
            Quantity = int.Parse(ItemsList.Rows[i].ItemArray[2].ToString()),
            Remaining = int.Parse(ItemsList.Rows[i].ItemArray[3].ToString()),
            Cost = int.Parse(ItemsList.Rows[i].ItemArray[4].ToString())
        });

    }
    Items = ItemsModel;
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    var handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}

Model.cs文件:

class ItemsModel
{
    public int SlNo        { get; set; }
    public int Quantity    { get; set; }
    public int Cost        { get; set; }
    public int Remaining   { get; set; }
    public String ItemName { get; set; }
}

如果AutoGenerateColumns="True"它创建了列并将数据添加到那些创建的列中,如:

enter image description here

如果为false,则DataGrid为空。

这是我正确的做法吗?为什么它不绑定到已创建的列?

2 个答案:

答案 0 :(得分:3)

你错过了:

Binding="{Binding Description}"

每个列定义的属性。您必须将它们显式链接到您的模型类属性。

答案 1 :(得分:1)

像Digitlafront说的那样,你需要设置绑定

<DataGrid ItemsSource="{Binding Items}" x:Name="ItemsDataGrid" Margin="0,30,0,0"
         AutoGenerateColumns="False">
   <DataGrid.Columns>
     <DataGridTextColumn Binding="{Binding SlNo, Mode=OneWay}" Header="Sl. No." MinWidth="125" />
     <DataGridTextColumn Binding="{Binding ItemName}" Header="Name" MinWidth="200" />
     <DataGridTextColumn Binding="{Binding Quantity} Header="Total Quantity" MinWidth="200" />
     <DataGridTextColumn Binding="{Binding Remaining} Header="Remaining" MinWidth="200" />
     <DataGridTextColumn Binding="{Binding Cost} Header="Cost/Each" MinWidth="200" />
   </DataGrid.Columns>
  </DataGrid>

你也应该根据需要设置Mode = TwoWay或OneWay。 我将使用ObservableCollection而不是List。