How to maintain a color for a single cell while selecting a Row and the NavigationMode is set in 'Row'

Hi,

I've a sfdatagrid populated with a observable collection and in 'Querycellstyle' event a single cell change color according to its value like image below:

Image_9885_1727953573998

If I select the row, the color is setted for all the row and I lost the previous color of cell

How can I keep the color of these cells even when I select the row? 


Thanks for your support.

Regards


3 Replies

SG Santhosh Govindasamy Syncfusion Team October 4, 2024 04:20 PM UTC

Hi Maurizio,

Thank you for reaching out.

From the provided information and the image you shared, as per understanding,you want to retain the custom background color of individual cells when selecting the entire row. Specifically, you're using the QueryCellStyle event to change the cell back color based on its value, but upon row selection, the color is lost.


To achieve the desired behavior where the cell back color is preserved even when selecting the row, you can handle the DrawCell event. This allows you to redraw the cell and set the back color based on the column name and row data values.


Below is an updated code snippet to help you:

sfDataGrid.DrawCell += SfDataGrid_DrawCell;

  private void SfDataGrid_DrawCell(object sender, Syncfusion.WinForms.DataGrid.Events.DrawCellEventArgs e)

  {

     

      if(e.Column.MappingName== "CustomerID" && (e.DataRow.RowData as OrderInfo).CustomerID == "ALFKI")

      {

          e.Style.BackColor = Color.Red;

          e.Style.TextColor = Color.White;

        

          StringFormat format = new StringFormat();

 

          DrawingHelper drawingHelper = new DrawingHelper();

 

          format.Alignment = (StringAlignment)e.Style.HorizontalAlignment;

          format.LineAlignment = (StringAlignment)e.Style.VerticalAlignment;

 

          e.Graphics.FillRectangle(new SolidBrush(e.Style.BackColor), e.Bounds);

 

          e.Graphics.DrawString(e.DisplayText, e.Style.GetFont(), new SolidBrush(e.Style.TextColor), e.Bounds, format);

 

          e.Graphics.DrawLine(new Pen(e.Style.Borders.Right.Color), e.Bounds.Right - 1, e.Bounds.Top, e.Bounds.Right - 1, e.Bounds.Bottom);

 

          e.Graphics.DrawLine(new Pen(e.Style.Borders.Bottom.Color), e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);

 

          // Handling the event

 

          e.Handled = true;

 

      }

  }


With this approach, the specific cell will retain its back color even when the row is selected. The key is to handle the DrawCell event, which allows you to override the default styling,when a row is selected.


Please review the attached modified sample for further reference. Let me know if you need any additional assistance.

Regards,
Santhosh.G


Attachment: ObservableCollection_fbb647ed.zip


MA Maurizio October 11, 2024 02:58 PM UTC

Hi, 

with your reply I've solved the problem.


Thanks

Regards



SG Santhosh Govindasamy Syncfusion Team October 11, 2024 03:14 PM UTC

Hi Maurizio,

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.


Regards,
Santhosh.G


Loader.
Up arrow icon