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
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;
}
}
} |
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;
}
}
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; }
}
} |
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;
}
}
} |