Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WPF, how can I bind a datagrid column to a specific column of a datatable?

I have a Datatable with many columns, and a datagrid in which I need to display only a few of those columns. I need a codesample of how to do it. I found a whole bunch of examples telling me to turn AutoGenerateColumns to true and set the table as DataContext to display all the columns in my DataTable. And I guess I could then put in a page of code to hide all the columns that I don't need, rearrange the leftover ones to proper order and size, but surely there must be a more elegant way.

In designer I have build the collection of columns I want to display, I got the datable, how do I bind an existing datagrid column to a specific datatable column in my code?

like image 1000
Andrey Avatar asked Sep 02 '10 17:09

Andrey


People also ask

How do I sort a column in DataGrid WPF?

WPF DataGrid (SfDataGrid) allows you to sort the data against one or more columns either in ascending or descending order. The sorting can be performed by clicking a column header. You can enable/disable the sorting for all the columns in DataGrid by using DataGrid. AllowSorting property.

What is the difference between grid and DataGrid in WPF?

A Grid is a control for laying out other controls on the form (or page). A DataGrid is a control for displaying tabular data as read from a database for example.

Can user add rows DataGrid WPF?

WPF DataGrid (SfDataGrid) provides built-in row called AddNewRow. It allows user to add a new row to underlying collection. You can enable or disable by setting SfDataGrid.


2 Answers

Bind your DataTable to the DataGrid, set the AutoGenerateColumns to False, and then add whatever columns you want to DataGrid.Columns. Here's an example using a DataTable called Collection, with two columns: ID and Value.

<DataGrid
    ItemsSource="{Binding Path=Collection}"
    AutoGenerateColumns=False
    >

    <DataGrid.Columns>
        <DataGridTextColumn Header="Id" Binding="{Binding Path=Id}" />
        <DataGridTextColumn Header="Value" Binding="{Binding Path=Value}" />
    </DataGrid.Columns>
</DataGrid>
like image 87
Rachel Avatar answered Sep 27 '22 21:09

Rachel


XAML binding would not work for me because my DataTable is generated at runtime so there is nothing to bind to at design time. Anyway, I stumbled on how to do what I wanted here.

My problems were because for some reason there was no error thrown when I would set ItemSource to the table itself, instead of the table's DefaultView, even though, as I later read, the table does not implement the required interface. But without any errors to tell me difference I had nothing to go on to find why my grid would display as empty.

DataGridName.DataContext = DataSetName.DataTableName;
DataGridName.ItemsSource = DataSetName.DataTableName.DefaultView;

((DataGridTextColumn)DataGridName.Columns[1]).Binding = new Binding("DataTableColumnName1");
((DataGridTextColumn)DataGridName.Columns[0]).Binding = new Binding("DataTableColumnName2");
((DataGridTextColumn)DataGridName.Columns[2]).Binding = new Binding("DataTableColumnName3");
like image 37
Andrey Avatar answered Sep 27 '22 22:09

Andrey