Hello, i want to change the background color of one cell in selected row when pressing ctrl + J. How can i do this?
Hi Christoph Beerwald,
Your requirement to change the back color of the last cell in the selected row on KeyPress
in SfDataGrid can be achieved by customizing the PreviewKeyDown and SelectionChanged
events. Refer to the below code snippet,
XAML Code Snippet:
<Application.Resources> <local:SelectorClass x:Key="styleSelector"/> <Style x:Key="highlightCell" TargetType="syncfusion:GridCell"> <Setter Property="Background" Value="Red" /> </Style> </Application.Resources>
<syncfusion:SfDataGrid x:Name="sfDataGrid" ItemsSource="{Binding Orders}" SelectionMode="Extended" CellStyleSelector="{StaticResource styleSelector}" AutoGenerateColumns="False"/> |
C# code snippet for updating the cell style at runtime:
//Event customization private void OnSelectionChanged(object sender, GridSelectionChangedEventArgs e) { if(e.RemovedItems.Count > 0) { foreach (var record in e.RemovedItems) { //If you need to remove the applied style in SfDataGrid for the selection of removed records //by calling the UpdateDataRow method to update the style at runtime in SfDataGrid this.AssociatedObject.UpdateDataRow((record as GridRowInfo).RowIndex); } } }
//Event customization private void OnPreviewKeyDown(object sender, KeyEventArgs e) { //Here customize based on your scenario if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) && e.Key == Key.J) { foreach(var record in this.AssociatedObject.SelectedItems) { //here get the RowIndex of the updated cell value var rowIndex = this.AssociatedObject.ResolveToRowIndex(record); //Change the Row Style at runtime by updating the value changed row index in SfDataGrid. this.AssociatedObject.UpdateDataRow(rowIndex); } } } |
UG Link: https://help.syncfusion.com/wpf/datagrid/helpers
https://help.syncfusion.com/wpf/datagrid/selection#selectionchanged-event
C# Code Snippet for StyleSelector:
public class SelectorClass : StyleSelector { public override Style SelectStyle(object item, DependencyObject container) { var gridCell = container as GridCell;
if (gridCell != null && gridCell.ColumnBase != null) { var column = gridCell.ColumnBase.GridColumn;
if (column != null) { var sfDataGrid = column.GetType().GetProperty("DataGrid", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(column) as SfDataGrid;
if (sfDataGrid != null) { foreach (var record in sfDataGrid.SelectedItems) { var rowIndex = sfDataGrid.ResolveToRowIndex(record);
if (gridCell.ColumnBase.RowIndex == rowIndex && gridCell.ColumnBase.ColumnIndex == sfDataGrid.GetLastColumnIndex()) { //Here apply the style based on your scenario return App.Current.Resources["highlightCell"] as Style; } } } } }
return base.SelectStyle(item, container); } } |
For more information related to conditional styling, please refer to the below
user guide documentation link,
UG Link: https://help.syncfusion.com/wpf/datagrid/conditional-styling?cs-save-lang=1&cs-lang=xaml#conditional-styling-of-cells-using-style-selector
https://help.syncfusion.com/wpf/datagrid/conditional-styling?cs-save-lang=1&cs-lang=xaml#condition-styling-of-cells-based-on-record-using-converter
https://help.syncfusion.com/wpf/datagrid/conditional-styling?cs-save-lang=1&cs-lang=xaml#conditional-styling-of-rows-using-style-selector
KB Link: https://www.syncfusion.com/kb/5992/how-to-change-the-gridcell-style-at-runtime
Find the sample in the attachment.
Regards,
Vijayarasan S
If this post is helpful, please consider Accepting it as the
solution so that other members can locate it more quickly.
Hello, thanks for the quick help. I still can't get any further. now it says the assembly reference to "local:SelectorClass" is missing. unfortunately i don't know which reference to add.
is it possible that i can return a color? I would like to change the color green by pressing the "ctrl + j" keys, red by "ctrl+n" and white by "ctrl+x".
Thank you!
Christoph
Beerwald,
Find the responses to your queries below.
Queries |
Responses |
|
Hello, thanks for the quick help. I still can't get any further. now it says the assembly reference to "local:SelectorClass" is missing. unfortunately i don't know which reference to add. |
|
|
is it possible that i can return a color? I would like to change the color green by pressing the "ctrl + j" keys, red by "ctrl+n" and white by "ctrl+x".
|
|
Find the modified sample in the attachment.
If this post is helpful, please consider Accepting it as the solution so that
other members can locate it more quickly.
That was exactly what i was looking for, thank you very much!
Christoph
Beerwald,
We are glad to know that the reported problem has been resolved at your end.
Please let us know if you have any further queries on this. We are happy to
help you😊.
Hello,
how can i export a sfdatagrid as excelfile with colored cells and import excelfile to sfdatagrid with colored cells?
Christoph, Please follow the newly created ticket for further updates