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. Image for the cookie policy date

Change cell style by right-button click on SfDataGrid Cell

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?

5 Replies

JG Jai Ganesh S Syncfusion Team April 28, 2016 05:28 PM UTC

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


VP Valery Piashchynski May 1, 2016 08:04 PM UTC

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.


JG Jai Ganesh S Syncfusion Team May 2, 2016 12:40 PM UTC

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 



VP Valery Piashchynski May 2, 2016 07:29 PM UTC

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


JG Jai Ganesh S Syncfusion Team May 3, 2016 12:49 PM UTC

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 


Loader.
Up arrow icon