We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Change cell style by right-button click on SfDataGrid Cell

Thread ID:

Created:

Updated:

Platform:

Replies:

123856 Apr 27,2016 06:46 AM May 3,2016 08:49 AM WPF 5
loading
Tags: SfDataGrid
Valery Piashchynski
Asked On April 27, 2016 06:46 AM

Hello, can you explain me how to do following (I am expect that it is easy): I have SfDataGrid and RecordContextMenu, in SfDataGrid option SelectionUnit = Cell. When I click right-button mouse by SfDataGrid opens context menu, in menu situated option - "change color". The question is following, how I can change color of only one cell?

Jai Ganesh S [Syncfusion]
Replied On April 28, 2016 01:28 PM

Hi Valery, 
 
You can achieve your requirement for change the cell color when clicking the RecordContextMenu by using the below code, 
 
 
<Style x:Key="cellStyle" TargetType="syncfusion:GridCell"> 
    <Setter Property="Background" Value="LightBlue" /> 
 </Style> 
 
 
<syncfusion:SfDataGrid.RecordContextMenu> 
                <ContextMenu Style="{x:Null}"> 
                    <MenuItem Command="{Binding Source={x:Static Member=local:ContextMenuCommands.Color}}" 
                                      CommandParameter="{Binding}" 
                                      Header="ChangeColor"> 
 
                    </MenuItem> 
                     
                 </ContextMenu> 
</syncfusion:SfDataGrid.RecordContextMenu> 
 
 
 
private static void OnColorClicked(object obj) 
        { 
            if (obj is GridRecordContextMenuInfo) 
            { 
                var grid = (obj as GridRecordContextMenuInfo).DataGrid; 
                var currentcell = grid.SelectionController.CurrentCellManager.CurrentCell; 
 
                if (currentcell == null) 
                    return; 
 
                var cell = (currentcell.Renderer as GridCellRendererBase).CurrentCellElement; 
   &nb

Valery Piashchynski
Replied On May 1, 2016 04:04 PM

Thx, it helps. But i have another one question. If i change (for example) cell backgroung property due programm is working, how can i save this state of cell? After restarting all cells are at default style. I am try BinaryFormatter/XmlSerializer/SfDataGrid.Serialize but it dont work.

Jai Ganesh S [Syncfusion]
Replied On May 2, 2016 08:40 AM

Hi Valery, 
 
We have analyzed your query. In the sample, you have changed the Grid cell style at run time. And this changes are maintaining the Underlying collection. In this case you have own to serialize the setting in sample level and no need to serialize the DataGrid. 
 
Regards, 
Jai Ganesh S 


Valery Piashchynski
Replied On May 2, 2016 03:29 PM

Thank you for answer. But i am change color of cell not by Style in StyleSelector (like in you sample) i change directly Cell.Background property because i need to mark cell by one of 3 different colors selected in SfDataGridRecordContextMenu.
If you can, explain, in what collection these values are stored? (to serialize it) Or i must create own collection with cells properties?

How i change cell color:
private static void AddGreenColor(object obj)
        {
            GridRecordContextMenuInfo info = obj as GridRecordContextMenuInfo;

            if (info == null)
                throw new ArgumentNullException(nameof(info));

            SfDataGrid grid = ((GridRecordContextMenuInfo)obj).DataGrid;

            DataColumnBase currentcell = grid.SelectionController.CurrentCellManager.CurrentCell;
            FrameworkElement cell = (currentcell.Renderer as GridCellRendererBase)?.CurrentCellElement;
            cell.SetValue(Control.BackgroundProperty, Brushes.Green);
            grid.UpdateDataRow(currentcell.RowIndex);
        }
Thank you

Jai Ganesh S [Syncfusion]
Replied On May 3, 2016 08:49 AM

Hi Valery, 
 
In your code snippet, you have directly applied the cell style while the cell is pressed. In this case the values are not stored anywhere and you own create a collection to store the cell values. For the below code snippet, we have stored the column index in ColoredRows collection like that you can create the own collection and store the cell values. 
 
private static void OnColorClicked(object obj) 
        { 
            if (obj is GridRecordContextMenuInfo) 
            { 
                var grid = (obj as GridRecordContextMenuInfo).DataGrid; 
                var currentcell = grid.SelectionController.CurrentCellManager.CurrentCell; 
                
 
                if (currentcell == null) 
                    return; 
 
                var cell = (currentcell.Renderer as GridCellRendererBase).CurrentCellElement; 
 
                var vm = cell.DataContext as OrderInfo; 
                 
                if (cell != null) 
                { 
                    //assigned columnindex to the index property in OrderInfo  
                    vm.DataIndex = currentcell.ColumnIndex; 
 
                    if (!vm.ColoredRows.Contains(vm.DataIndex)) 
                        vm.ColoredRows.Add(vm.DataIndex); 
 
                    cell.SetValue(Control.BackgroundProperty, Brushes.Green); 
                    //updates the current row index 
                    grid.UpdateDataRow(currentcell.RowIndex); 
                } 
            } 
        } 
 
Regards, 
Jai Ganesh S 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;