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 rendered grid row backcolor

Hello,

It is possidble to change a gridrow backcolor after the grid is rendered? (so not trough grid events etc..)

I have two grid (primary, secondary) and I need change the backcolor of specified rows in the secondary table  based on selection in the primary table. Every row selection in the primary table causes backcolor change in the secondary table in specified rows.

That is possible?

Thank you very much for help!

Best Regards, SZL


5 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team April 25, 2023 02:04 PM UTC

Hi SZL,

Your requirement to change the back color of specified rows in the secondary table based on selection in the primary table can be achieved by customizing the SelectionChanged event and set the back color for selected items in secondary table. Refer to the below code snippet,

// Event subscription

sfDataGrid.SelectionChanged += SfDataGrid_SelectionChanged;

//Event customization

private void SfDataGrid_SelectionChanged(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangedEventArgs e)

{

    // Customize based on your scenario

    // Set the Selected item for second SfDataGrid

    sfDataGrid1.SelectedItem = sfDataGrid.SelectedItem;

}

 

// Set the Selection color for Second Grid

this.sfDataGrid1.Style.SelectionStyle.BackColor = Color.LightSeaGreen;


UG Link: https://help.syncfusion.com/windowsforms/datagrid/selection#appearance

https://help.syncfusion.com/windowsforms/datagrid/selection#programmatic-selection

https://help.syncfusion.com/windowsforms/datagrid/selection#displaying-message-box-on-selection-changed

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_6d1129ee.zip


SZ SZL replied to Vijayarasan Sivanandham April 25, 2023 09:33 PM UTC

Thank you very much for the example!

A little problem with this, that I not want to modify the SelectedItems collection in second grid (the row backcolor change is only a highlight for the end-user).





VS Vijayarasan Sivanandham Syncfusion Team April 26, 2023 05:08 PM UTC

SZL,

Your requirement can be achieved by customizing the SelectionChanged and QueryRowStyle events. Refer to the below code snippet,

// Event subscription

 sfDataGrid.SelectionChanged +=OnSelectionChanged;

 // Event subscription

 sfDataGrid1.QueryRowStyle += OnQueryRowStyle;    

 

//Event customization

private void OnQueryRowStyle(object sender, QueryRowStyleEventArgs e)

{

    // Customize based on your scenario

    if(sfDataGrid.SelectedItem != null)

    {

        // Customize based on your scenario

        if (isAddedSelected && e.RowData == sfDataGrid.SelectedItem)

        {

            // Set the backGround color for the row

            e.Style.BackColor = Color.LightBlue;

            isAddedSelected = false;

        }

        // if you want to remove the backcolor of row, set the white color for selected items

        else if (isRemovedSelected)

        {

            // Reset the backGround color for the row

            e.Style.BackColor = Color.White;

            isRemovedSelected = false;

        }

 

    }

}

 

// Maintain the flag for selection related backcolor applied in row

bool isAddedSelected;

bool isRemovedSelected;

 

//Event customization

private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)

{

   

    // Customize based on your scenario          

    if (e.AddedItems.Count > 0)

    {

        isAddedSelected = true;

        foreach (var item in e.AddedItems)

        {

            // Get the row index of the selected item

            var rowIndex = sfDataGrid1.TableControl.ResolveToRowIndex(item);

            // Invalidate the row to refresh the row

            sfDataGrid1.TableControl.Invalidate(sfDataGrid1.TableControl.GetRowRectangle(rowIndex, true));

        }

      

    }

 

    // if you want to remove the backcolor of selection removed row, here refresh the row

    if (e.RemovedItems.Count > 0)

    {

        isRemovedSelected = true;

        foreach (var item in e.RemovedItems)

        {       

            // Get the row index of the selection removed item

            var rowIndex = sfDataGrid1.TableControl.ResolveToRowIndex(item);

            // Invalidate the row to refresh the row

            sfDataGrid1.TableControl.Invalidate(sfDataGrid1.TableControl.GetRowRectangle(rowIndex, true));

        }

    

    }

}


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

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_4f0730a0.zip

Marked as answer

SZ SZL replied to Vijayarasan Sivanandham May 1, 2023 10:58 AM UTC

Thank you very much, it will be fine!


Best Regards,

László



VS Vijayarasan Sivanandham Syncfusion Team May 2, 2023 06:29 AM UTC

SZL,

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😊.


Loader.
Live Chat Icon For mobile
Up arrow icon