Hi Edson Nilo,
I think give you a sample can better explain my meaning. Please look at the following sample.
public partial class Form1 : Form
{
private DataTable dt = new DataTable();
private bool firstRun = true;
public Form1()
{
InitializeComponent();
dt.Columns.Add("Column");
for (int i = 0; i < 10; i++)
{
dt.Rows.Add(i);
}
dataGridView1.DataSource = dt;
dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
}
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (firstRun)
{
foreach (DataGridViewRow dgvRow in dataGridView1.Rows)
{
if (dgvRow.IsNewRow == false && dgvRow.Cells[0].Value.ToString() == "1")
{
dgvRow.Cells[0].Style.BackColor = Color.Red;
}
}
firstRun = false;
}
}
void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.FormattedValue.ToString() == "1")
{
dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Red;
}
}
}
In this sample, I will set the cell's backcolor to Red whose value is 1, So I handle the CellValidating event to do the check and set. But only handle that event is not enough. When the form is load, you can see the cell whose value is 1 doesn't have red backcolor. That's the reason why I handle DataBindingComplete event to set the color at the first time.
Do you understand my meaning?
Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the
All-In-One Code Framework!