sfdatagrid background color

Gents,

I have this problem. I want to iterate row values of specific datagrid column.

here is how I push my value from datatable to sfdatagrid.

DataTable table = new DataTable();

sfdatagrid.DataSource = table;

var records = dataGridView1.View.Records;
            foreach (var record in records)
            {
                var dataRowView = record.Data as DataRowView;
                if (dataRowView != null)
                {
                    var selectedValue = dataRowView.Row["Category"];
                    var selected = dataRowView.Row["Apple"];
                    if (selected.GetType() != typeof(DBNull) && (bool)selected)
                    {
                        dataGridView1.BackColor = sys.Drawing.Color.Red;
                    }
                  
                }
            }

i get this error: Column "Apple" does not belong to the table

could you please provide me a working code. my intent is to color the backgound of my cell according to their group name.


thank you


7 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team June 25, 2020 05:39 PM UTC

Hi Mark Jayvee,

Thank you for contacting Syncfuion support.
 
Your requirement can be achieved by using SfDataGrid.QueryRowStyle. Please refer the below code snippet. Please refer the below code snippet,

 
sfDataGrid1.QueryRowStyle += SfDataGrid1_QueryRowStyle;

private void SfDataGrid1_QueryRowStyle(object sender, Syncfusion.WinForms.DataGrid.Events.QueryRowStyleEventArgs e) 
{ 
            // Get the "Country" column from the RowData    
            if (e.RowType == RowType.DefaultRow) 
            { 
                var dataRowView = e.RowData as DataRowView; 
                if (dataRowView != null) 
                { 
                    var dataRow = dataRowView.Row; 
                    var cellValue = dataRow["Color"].ToString(); 
 
                    if (cellValue == "Red") 
                        e.Style.BackColor = Color.Red; 
                    else if (cellValue == "Green") 
                        e.Style.BackColor = Color.Green; 
                    else if (cellValue == "Blue") 
                        e.Style.BackColor = Color.Blue; 
                } 
            } 
} 
For more information, please refer the below UG link,

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

We hope this helps. Please let us know, if you require further assistance on this. 
Regards,
Vijayarasan S
 


Marked as answer

MJ Mark Jayvee June 26, 2020 10:25 AM UTC

hi thank you for the response. code works when form.show();

what I am looking is the background color will change when pressing a button.

trying this code, but no luck. could you please advise.

var records = dataGridView1.View.Records;
            foreach (var record in records)
            {
                var dataRowView = record.Data as DataRowView;
                if (dataRowView != null)
                {
                    var dataRow = dataRowView.Row;
                   
                    var cellValue = dataRow["Apple"].ToString();
                  
                    dataGridView1.BackColor = sys.Drawing.Color.RoyalBlue;


                }
            }





VS Vijayarasan Sivanandham Syncfusion Team June 29, 2020 04:47 PM UTC

Hi Mark Jayvee,

Thanks for the update.

Your requirement can be achieved by using  CellStyle property in SfDataGrid. Please refer the below code snippet,
 
private void button1_Click(object sender, EventArgs e) 
{ 
            var records = this.sfDataGrid1.View.Records; 
 
            foreach (var record in records) 
            {                 
                var dataRowView = record.Data as DataRowView; 
                if (dataRowView != null) 
                { 
                    var dataRow = dataRowView.Row; 
                    var cellValue = dataRow["Color"].ToString(); 
                    this.sfDataGrid1.Style.CellStyle.BackColor = Color.Red;
                    this.sfDataGrid1.Style.HeaderStyle.BackColor = Color.Green;                   
                } 
            } 
} 
UG Link: https://help.syncfusion.com/windowsforms/datagrid/styling#styling-record-cell

Regards,
Vijayarasan S





MJ Mark Jayvee June 30, 2020 09:27 AM UTC

hi Vijarasan,

thank you for the response. now it works. referring to your example.
at column "Color", I want to apply your color code per country. 

will that be possible?

thank you


VS Vijayarasan Sivanandham Syncfusion Team July 1, 2020 05:56 PM UTC

Hi Mark Jayvee,

Thanks for the update.

As we mentioned earlier
Your requirement can be achieved by using SfDataGrid.QueryRowStyle event when this event used for Row Style applied initial state. If you want to apply after datagrid loaded via button click. based on our SfDatagrid source architecture it is not possible to apply rowstyle. Please refer the below code snippet, 
this.sfDataGrid1.QueryRowStyle += SfDataGrid1_QueryRowStyle;
 
private void SfDataGrid1_QueryRowStyle(object sender, Syncfusion.WinForms.DataGrid.Events.QueryRowStyleEventArgs e) 
{ 
            if (e.RowType == RowType.DefaultRow) 
            { 
                var dataRowView = e.RowData as DataRowView; 
                if (dataRowView != null) 
                { 
                    var dataRow = dataRowView.Row; 
                    var cellValue = dataRow["Country"].ToString(); 
 
                    if (cellValue == "Germany") 
                        e.Style.BackColor = Color.Red; 
                    else if (cellValue == "Mexico") 
                        e.Style.BackColor = Color.Green; 
                    else if (cellValue == "Spain") 
                        e.Style.BackColor = Color.Blue; 
                } 
            } 
} 
 
We hope this helps. Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S
 



MJ Mark Jayvee July 1, 2020 06:16 PM UTC

Thank you for clarification.


VS Vijayarasan Sivanandham Syncfusion Team July 2, 2020 04:57 PM UTC

Hi Mark Jayvee,

Thanks for the update.

You are welcome. please get back to us if you need any other assistance.

Regards,
Vijayarasan S
 


Loader.
Up arrow icon