Date formatting in WPF datagrid

Don`t forget to use DataGrid.Columns, all columns must be inside that collection. In my project I format date a little bit differently: <tk:DataGrid> <tk:DataGrid.Columns> <tk:DataGridTextColumn Binding=”{Binding StartDate, StringFormat=\{0:dd.MM.yy HH:mm:ss\}}” /> </tk:DataGrid.Columns> </tk:DataGrid> With AutoGenerateColumns you won`t be able to contol formatting as DataGird will add its own columns.

Get selected row item in DataGrid WPF

You can use the SelectedItem property to get the currently selected object, which you can then cast into the correct type. For instance, if your DataGrid is bound to a collection of Customer objects you could do this: Customer customer = (Customer)myDataGrid.SelectedItem; Alternatively you can bind SelectedItem to your source class or ViewModel. <Grid DataContext=”MyViewModel”> … Read more

programmatically add column & rows to WPF Datagrid

To programatically add a row: DataGrid.Items.Add(new DataItem()); To programatically add a column: DataGridTextColumn textColumn = new DataGridTextColumn(); textColumn.Header = “First Name”; textColumn.Binding = new Binding(“FirstName”); dataGrid.Columns.Add(textColumn); Check out this post on the WPF DataGrid discussion board for more information.

WPF Error: Cannot find governing FrameworkElement for target element

Sadly any DataGridColumn hosted under DataGrid.Columns is not part of Visual tree and therefore not connected to the data context of the datagrid. So bindings do not work with their properties such as Visibility or Header etc (although these properties are valid dependency properties!). Now you may wonder how is that possible? Isn’t their Binding … Read more

Button in a column, getting the row from which it came on the Click event handler

Basically your button will inherit the datacontext of a row data object. I am calling it as MyObject and hope MyObject.ID is what you wanted. private void Button_Click(object sender, RoutedEventArgs e) { MyObject obj = ((FrameworkElement)sender).DataContext as MyObject; //Do whatever you wanted to do with MyObject.ID }

How do I bind a WPF DataGrid to a variable number of columns?

Here’s a workaround for Binding Columns in the DataGrid. Since the Columns property is ReadOnly, like everyone noticed, I made an Attached Property called BindableColumns which updates the Columns in the DataGrid everytime the collection changes through the CollectionChanged event. If we have this Collection of DataGridColumn’s public ObservableCollection<DataGridColumn> ColumnCollection { get; private set; } … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)