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
Thank you for contacting Syncfuion support.
|
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;
}
}
} |
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.
Vijayarasan S
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;
}
}
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; }
}
} |
Screenshot for your reference:
Sample Link: https://www.syncfusion.com/downloads/support/forum/155479/ze/Sample-778050250
Regards,
Vijayarasan S
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;
}
}
} |
Regards,
Vijayarasan S
Thanks for the update.
You are welcome. please get back to us if you need any other assistance.
Regards,
Vijayarasan S
- 7 Replies
- 2 Participants
- Marked answer
-
MJ Mark Jayvee
- Jun 24, 2020 10:29 AM UTC
- Jul 2, 2020 04:57 PM UTC