multiple selection colors

good evening,


it is possible i can set multiple color (two colors) in selected rows in sfdatagrid for winforms?

I have try to convert

https://support.syncfusion.com/kb/article/6520/how-to-apply-multiple-selection-colors-in-sfdatagrid

for me, but i don't found GridSelectionController in any winforms-class and i also don't found a  alternative class :(

I hope its possible?

many greetings from germany

Daniel


5 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team June 12, 2023 03:04 PM UTC

Hi Daniel,


We are a little unclear about your scenario. If your requirement is setting the multiple colors for the rows that are available in the SfDataGrid. it will be achievable by using SfDataGrid.QueryRowStyle event as shown in the below documentation,


UG Link: https://help.syncfusion.com/windowsforms/datagrid/conditionalstyling#styling-based-on-content-1


If you need to apply different colors to selected rows, we would need more information on the criteria or conditions for applying the colors. Could you please provide details on the conditions based on which you want to apply the colors? This will help us better understand your requirements and provide you with an appropriate solution.




DA Daniel June 12, 2023 05:29 PM UTC

Hi,

of course, i have more information :)

I need different color for selected rows.

Example: i select the first 5 rows (mouse or keyboard, selectionmode is multiple and selctionunit is row), row 1,3,5 have the color blue and 2,4 have the color green. The colors are always changing, blue green blue green blue.




VS Vijayarasan Sivanandham Syncfusion Team June 13, 2023 02:49 PM UTC

Hi Daniel,

We have checked the feasibility to achieve your requirement from our end. Whenever you are performing the multiple selection our SfDataGrid default selection back color will be applied.  Our SfDataGrid allows you to change the selection background as shown in the below documentation link,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/selection#change-the-background-and-foreground-color-for-selection

Unfortunately, there is no direct support for applying multiple colors for the selected rows in SfDataGrid. However, we have created a workaround to achieve your requirement by customizing the DrawCell and QueryRowStyle events. Refer to the below code snippet,

// Event subscription

sfDataGrid1.DrawCell += OnDrawCell;          

sfDataGrid1.QueryRowStyle += OnQueryRowStyle;

 

//Event customization back color for selected items

private void OnQueryRowStyle(object sender, QueryRowStyleEventArgs e)

{

    // Check selected items contains or not

    if (sfDataGrid1.SelectedItems.Contains(e.RowData))

    {

        foreach (var item in sfDataGrid1.SelectedItems)

        {

            // Check the condition based on Selected item and row data is same

            if (e.RowData == item)

            {

                // Customize based on your requirement

                if (e.RowIndex % 2 == 0)

                    e.Style.BackColor = Color.Green;

                else

                    e.Style.BackColor = Color.Blue;

            }

 

        }

    }

    else // Default style for other rows

        e.Style.BackColor = Color.Transparent;

 

}

 

//Event customization for selected items default style

private void OnDrawCell(object sender, DrawCellEventArgs e)

{

    // Check selected items contains or not

    if(e.DataRow != null && e.DataRow.RowType == RowType.DefaultRow && sfDataGrid1.SelectedItems != null)

    {

        //Handle the default style for selected items in SfDataGrid

        foreach(var item in sfDataGrid1.SelectedItems)

        {

            if(e.DataRow.RowData == item)

            {                         

                StringFormat format = new StringFormat();

                DrawingHelper drawingHelper = new DrawingHelper();

                if (e.Column.CellType == "Numeric")

                    e.Style.HorizontalAlignment = HorizontalAlignment.Right;

                var gethorizontalAlignment = drawingHelper.GetType().GetMethod("ConvertToStringAlignment", BindingFlags.NonPublic | BindingFlags.Static, Type.DefaultBinder, new Type[] { typeof(HorizontalAlignment) }, new ParameterModifier[] { }).Invoke(drawingHelper, new object[] { e.Style.HorizontalAlignment });

                var verticalAlignment = drawingHelper.GetType().GetMethod("ConvertToStringAlignment", BindingFlags.NonPublic | BindingFlags.Static, Type.DefaultBinder, new Type[] { typeof(VerticalAlignment) }, new ParameterModifier[] { }).Invoke(drawingHelper, new object[] { e.Style.VerticalAlignment });

                format.Alignment = (StringAlignment)gethorizontalAlignment;

                format.LineAlignment = (StringAlignment)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;

            }

        }

    }          

}


UG Link: https://help.syncfusion.com/windowsforms/datagrid/conditionalstyling#styling-alternate-rows

https://help.syncfusion.com/windowsforms/datagrid/conditionalstyling

Find the sample demo 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.


Attachment: SfDataGridDemo_e9b51eef.zip

Marked as answer

DA Daniel June 14, 2023 06:02 PM UTC

Hi,

thank you very much ... this is an good code snippet :)

I make only few little adjustments for color and alignment.




VS Vijayarasan Sivanandham Syncfusion Team June 15, 2023 05:45 AM UTC

Daniel,

If you are satisfied with our response, please mark it as an answer. Otherwise, please let us know if you have any further queries on this. We are happy to help you😊.


Loader.
Up arrow icon