FAQ Database Discussion Community


WPF: Programatically adding a ContextMenu to a DataGrid column header

c#,.net,wpf,datagrid,wpfdatagrid
I'm trying to add individualized and separate ContextMenus to each column header in my project, so that when a user right-clicks a header, a menu of checkboxes that relates to that header will appear that allows them to filter the data. A couple of catches: the project that I'm working...

Enable button when checkbox is checked in WPF datagrid

wpf,wpfdatagrid
I have a Datagrid in WPF in which first column has checkbox column and last column has buttons. Initially, I want to make all the buttons disabled and whenever any checkbox is checked then button of that row should get enabled. checkbox is unchecked then button should be disabled. Searched...

Does a WPF `DataRow` have a property for the ID or PK of the row item?

c#,wpf,data-binding,wpfdatagrid
I've done some looking around, and it seems there is no apparent means of telling a WPF DataGrid which property in the item bound to a DataRow, and nor is there a special field on the DataRow for this property. Using a special 'Key' column that is normally hidden is...

Binding warning when scrolling horizontally in datagrid

.net,wpf,visual-studio,binding,wpfdatagrid
In my WPF application, when I scroll horizontally a DataGrid, Visual Studio 2010 Output print this warning: System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='-0.29487179487171' BindingExpression:Path=CellsPanelHorizontalOffset; DataItem='DataGrid' (Name=''); target element is 'Button' (Name=''); target property is 'Width' (type 'Double') I'm looking for datagrid...

Get column name from (object sender, RoutedEventArgs e)

c#,wpf,wpfdatagrid
I would like to get the column name of the cell the same way i did with its content on my loose focus method.I can get the content but not the column header. private void lostFocus(object sender, RoutedEventArgs e) { var jj = sender as DataGridColumnHeader; var box = sender...

Keep focus on a row after DataGrid.ItemsSource changed

c#,wpf,datagrid,wpfdatagrid
I'd like to keep the focus on the selected row after the ItemsSource has changed. I think I'm almost done, but none of some accepted answers does work for me :( This is my code: public void UpdateItemsSource() { IdentifySelectedError(); //get "selectedRowIndex" DataGridRow dgr = (DataGridRow)DataGrid.ItemContainerGenerator.ContainerFromIndex(selectedRowIndex); using (var context =...

WPF Datagrid focus and highlighting last row

c#,wpf,wpfdatagrid,wpf-4.0
I would like to highlight my lastly new create row in my Data grid, I have reference this page http://social.technet.microsoft.com/wiki/contents/articles/21202.wpf-programmatically-selecting-and-focusing-a-row-or-cell-in-a-datagrid.aspx to get some idea. According to the reference link above datagrid first have to visualise . The reason of the last row is not focus and high light, might be...

Selecting and Focusing a Datagrid based on a variable

c#,focus,wpfdatagrid
I want to keep this short and sweet. I can't figure out how to set focus and go to a certain row based on a user input, either like TextBox or a ComboBox. I keep trying different ways to try and write it but with no success. I have a...

Column header backgound — but not twice

wpf,wpfdatagrid
I have the usual style: <Style TargetType="{x:Type DataGrid}"> <Setter Property="ColumnHeaderStyle"> <Setter.Value> <Style TargetType="{x:Type DataGridColumnHeader}"> <Setter Property="Background" Value="{x:Null}" /> <!-- this doesn't help, either --> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGridColumnHeader}"> <Border Background="SomeTransparentColor" CornerRadius="20" Margin="5" /> ... The problem is that the background is applied...

How to Add Rows to WPF dataGrid when “Add” button click?

c#,wpf,wpfdatagrid,datarow
Here am using the below code for adding rows to wpf datagrid,but if am again click to add means the row cannot be added and previous row replaced by new added details....So how to make possible to add new more rows based on add button click externally. Here the code...

How to Show percentage in WPF datagridProgressbarcolumn along with progress?

c#,wpf,wpfdatagrid,datatemplate,datarow
I am used the below codes in datagridtemplateprogressbarcolumn and its working fine progress But how to give the percentage along with this below code. The below code is working for progress only,How to show percentage along with that code. In xaml file:- <DataGridTemplateColumn Header="Progress" Width="*" Visibility="Visible"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ProgressBar Value="{Binding...

Reset DataGrid.SelectedIndex after user has selected another

c#,wpf,datagrid,wpfdatagrid
In the UI I'm working with, in a DataGrid, there are cases where the user might select a different row, but after a dialog interaction, the old row needs to be shown as being selected once again. If I simply try BundleQueueDG.SelectedIndex = currentBundleIndex; that does not do anything and...

how to change datagrid row background color with value taken from database in wpf

c#,wpf,wpfdatagrid
I want to change datagrid row background color, by taking value from Database. I have 2 value "ERROR" and "OK". If Column string value is ERROR then row color will be red and if OK then It must be Green. This value get from database by executing query. I have...

How to check every 2 minutes for report checking in datagrid wpf?

c#,wpf,multithreading,windows-services,wpfdatagrid
I want to check all the datas in database for the status is changed to "Reported" automatically refresh it in wpf datagrid Here the below code am using,stell me how to given this in a thread for automatically refresh every 2minutes. how to give the below code for automatic refresh...

DataGridRowEditEndingEventArgs 's row.item returning null

c#,wpf,xaml,data-binding,wpfdatagrid
I just created a new WPF MVVMLight Project, simple : MainWindow.xaml <Window x:Class="DG.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:command="http://www.galasoft.ch/mvvmlight" DataContext="{Binding Main, Source={StaticResource Locator}}"> <i:Interaction.Triggers>...

Create a DataGrid in WPF with column type equal with DataGridViewButtonColumn of WinForm

c#,wpf,datagridview,wpfdatagrid
In WinForm i can programmatically add Rows on a DataGridView column(s) with loop, let say like this private void rowsAdder() { for(int u = 0; u<= 10; u++) { classSelect.Rows.Add("Number " + u); //classSelect is a name of a DataGridView, where the first column is a DataGridViewButtonColumn } } Then...

Datagrid shows only one record

c#,wpf,datagrid,wpfdatagrid
everything looks fine from my point of view, but the dataGrid shows only the first item which has been added to the grid. The XAML part: <DataGrid HorizontalAlignment="Stretch" Name="dataGridIE" VerticalAlignment="Stretch" HeadersVisibility="All" CanUserReorderColumns="False" CanUserResizeColumns="False" AutoGenerateColumns="True" ColumnWidth="100" CanUserAddRows="True" CanUserDeleteRows="True" Height="97" Margin="23,38,16,38" Grid.ColumnSpan="2" Width="682"> <!--<DataGrid.Columns> <DataGridTextColumn Header="Key"...

How to use LostFocus as a Command in WPF

wpf,wpf-controls,wpfdatagrid
I am trying to use LostFocus event as Command in DataGridTextColumn and can't find an example on how to use it in WPF. Can anyone help me use it as I am new to WPF. Thank you. My xaml looks like: ` The c# code: public partial class Myogg :...

How to skip WPF DataGrid cell focus and go to control inside it?

wpf,wpfdatagrid
In WPF Project I have button in datagridcell when i press tab it will focus the cell first and then the button inside this cell how can i skip cell focus The columns are dynamic and i don't know which column has button or textbox

DataGridComboBoxColumn wont display selected item in cell on ending edit

c#,wpf,wpfdatagrid,datagridcomboboxcolumn
I'm having an issue when I select an item from a DataGridComboBoxColumn. The cell wont display the name of the item I've chosen after I move focus to the next cell. This is the code I have: DataGridComboBoxColumn cb1 = new DataGridComboBoxColumn(); cb1.ItemsSource = listOStrings; cb1.TextBinding = new Binding("listOfStrings"); e.Column...

Databinding Enum - The name 'StoreLocation' does not exist in the namespace

c#,wpf,enums,wpfdatagrid
I'm getting a error: The name 'StoreLocation' does not exist in the namespace "clr-namespace:System.Security.Cryptography.X509Certificates;assembly=mscorlib" on line: <x:Type TypeName="crypto:StoreLocation" /> What I'm doing wrong? <UserControl x:Class="Admin.Certificates" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Admin"...

Resize Columns of Styled WPF DataGrid

wpf,wpf-controls,wpfdatagrid,wpfstyle
I have DataGrid with custom style, I'm having trouble in re-sizing columns because it only works when the cursor is EXACTLY between the two columns Here's the part of style that sets the DataGridColumnHeader <Style TargetType="{x:Type DataGridColumnHeader}"> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="Foreground" Value="White"/> <Setter Property="FontFamily" Value="Segoue UI"/>...

Column Headers Offset From Data in Wpf Datagrid

c#,wpf,wpfdatagrid
Good morning I have a Wpf datagrid that is displaying an observable collection of a custom type I group the data using a collection view source in XAML on two seperate properties, and I have styled the groups to display as expanders. For clarity, as there is a lot of...

WPF DataGrid: Displaying 2 decimal places when not editing, but full number when editing

c#,wpf,datagrid,wpfdatagrid
im am trying to have a datagrid, where when the user enters the value and presses enter, it displays the data with 2 decimal points of precision. However when they click in to edit it, i want them to be able to view the whole number again. What i currently...

WPF DataGrid default sort not working

wpf,xaml,mvvm,datagrid,wpfdatagrid
I have a DataGrid, with columns XAML as such: <DataGridTextColumn Header="Time" Binding="{Binding Date, StringFormat='yyyy-MM-dd HH:mm:ss'}" SortMemberPath="Date" SortDirection="Descending" Width="130" CanUserResize="True" /> <DataGridTextColumn Header="Level" Binding="{Binding Level}" Width="60" CanUserResize="True" /> <DataGridTextColumn Header="Source" Binding="{Binding Logger}" Width="150" CanUserResize="True" /> <DataGridTextColumn Header="Message" Binding="{Binding Message}" Width="*" CanUserResize="True"...