Sort a wpf datagrid programmatically

voo’s solution was not working for me, ItemsSource was null, most likely because it was not directly set, but bound. All other solutions I found here at StackOverflow were dealing with sorting the Model only, but the DataGrid header was not reflecting to the sort. Here’s a proper solution based on the incomplete script here: … Read more

How do I dynamically generate columns in a WPF DataGrid?

Ultimately I needed to do two things: Generate the columns manually from the list of properties returned by the query Set up a DataBinding object After that the built-in data binding kicked in and worked fine and didn’t seem to have any issue getting the property values out of the ExpandoObject. <DataGrid AutoGenerateColumns=”False” ItemsSource=”{Binding Results}” … Read more

DataGrid row content vertical alignment

Complete solution of this issue at MSDN: Vertical alignment of DataGrid row content. In brief, in style-file set: <!–body content datagrid cell vertical centering–> <Style x:Key=”Body_Content_DataGrid_Centering” TargetType=”{x:Type DataGridCell}”> <Setter Property=”Template”> <Setter.Value> <ControlTemplate TargetType=”{x:Type DataGridCell}”> <Grid Background=”{TemplateBinding Background}”> <ContentPresenter VerticalAlignment=”Center” /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> In window file: <DataGrid x:Name=”ContentDataGrid” Style=”{StaticResource ContentDataGrid}” CellStyle=”{StaticResource Body_Content_DataGrid_Centering}” ItemsSource=”{Binding}” … Read more

How to build A WPF Datagrid with an unknown number of columns

There is DataTable class in .Net. Its primary purpose is to communicate with relational database but it can be used nicely to store, display and edit tabular data (e.g. read and display .csv/Excel files -> DataTable + DataGrid in wpf, DataTable + DataGridView in WinForms). DataTable columns (DataColumn) can be added/removed at runtime and DataGrid … Read more

Merge Cells in WPF DataGrid

Try use DataGridTemplateColumn. I created sample test class for databinding public class Test { public Test(string name, string attribute1, string attribute2) { Name = name; Attributes = new Attribute(attribute1, attribute2); } public string Name { get; set; } public Attribute Attributes { get; set; } } public class Attribute { public Attribute(string attribute1, string attribute2) … Read more

tech