Binding WPF DataGrid to DataTable using TemplateColumns

Edit: Updated to reflect the input of Aran Mulholland (see comment) Apparently the DataGrid is passing the entire DataRowView to each cell. That’s why the binding doesn’t work. Your DataTemplate expects the DataContext to be of type MyData, but instead it is of type DataRowView. My proposed (somewhat hack-ish) workaround to get the DataContext you … Read more

How to access datagrid template column textbox text WPF C#

To find a control in a DataGrid template column, you should use FindChild(): public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject { if (parent == null) { return null; } T foundChild = null; int childrenCount = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < childrenCount; i++) { var child = VisualTreeHelper.GetChild(parent, … Read more

WPF Datagrid Get Selected Cell Value

Please refer to the DataGrid Class page on MSDN. From that page: Selection By default, the entire row is selected when a user clicks a cell in a DataGrid, and a user can select multiple rows. You can set the SelectionMode property to specify whether a user can select cells, full rows, or both. Set … Read more

DataGrid column width doesn’t auto-update

The DataGrid will increase column sizes to fit as the data becomes longer, but it does not automatically decrease column sizes when the length of the data decreases. In your example, you’re right aligning the ‘Change’ column, and using the rest of the space for the ‘Name’ column. Now, when a ‘Change’ property grows large … Read more

Improve WPF DataGrid performance

There are a few options you can turn on to help you on your DataGrid object EnableColumnVirtualization = true EnableRowVirtualization = true These two are the main ones I think might help. Next try making your binding async ItemsSource=”{Binding MyStuff, IsAsync=True}” And lastly, I’ve heard that setting a maximum height and width can help even … Read more

DataGrid shows path of image instead of image itself

first of all, DataGrid generate DataGridTextColumns by default and we have to use AutoGeneratingColumn event to change type of column. We need to use DataGridTemplateColumn which contains Image in template (image source should be bound to correct DataTable column). The best place to define template is in Resources. here is how the issue can be … Read more

How can I set the color of a selected row in DataGrid

The above solution left blue border around each cell in my case. This is the solution that worked for me. It is very simple, just add this to your DataGrid. You can change it from a SolidColorBrush to any other brush such as linear gradient. <DataGrid.Resources> <SolidColorBrush x:Key=”{x:Static SystemColors.HighlightBrushKey}” Color=”#FF0000″/> </DataGrid.Resources>

tech