Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Sir,

i am fetching data from database.
I have tried upto here-

C#
private void button1_Click(object sender, EventArgs e)
{
try
{
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "select * from School";

OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

dataGridView1.DataSource = dt;

}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}

con.Close();
}


Please advise me how can i show then cells color is red (if value is 0) or green(if value is 1).

What I have tried:

Sir please advise me how to get the cells value from datagridview.
Posted
Updated 17-Apr-16 22:39pm
v2

Try this:

C#
private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
       {
           var dgv = (DataGridView) sender;

           if (e.RowIndex < 0)
               return;

           var rowCurrent = ((DataRowView) dgv.Rows[e.RowIndex].DataBoundItem).Row;

           e.CellStyle.BackColor = rowCurrent["yourValue"].ToString() == "0" 
               ? Color.White
               : Color.Red;
       }
 
Share this answer
 
Comments
Member 12466358 18-Apr-16 4:32am    
"your value" it means which value i will put here?
Each datagridview has rows and columns. "yourValue" means the datapropertyname of the column you want to check
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900