private void PopulateContacts()
{
adap = new OleDbDataAdapter("SELECT * FROM tblUsers ORDER BY UOnline, UName;", conn);
dt = new DataTable();
adap.Fill(dt);
this.dgvContacts.DataSource = dt;
dgvContacts.Columns[0].Visible = false;
dgvContacts.Columns[1].Width = dgvContacts.Width-3;
dgvContacts.Columns[2].Visible = false;
dgvContacts.Columns[3].Visible = false;
for (int i = 0; i < dgvContacts.RowCount; i++)
{
if (dgvContacts[1, i].Value.ToString() == "True")
{
dgvContacts[2, i].Style.BackColor = Color.Red;
}
else
{
dgvContacts[2, i].Style.BackColor = Color.MediumBlue;
}
}
}
The query returns 2 values. one has a value of online = true, the other is online = false so id expect one to be blue, the other to be red. both appear white and are not changing.
the code doesnt give a warning or anything and i've used this code before wheere it worked fine. i'm completely stumped as to why its now not working in this program.
Thanks
Jignesh TrivediPosted Jul 5, 2012, 12:16 AM
The Good idea to change row style is use RowData Bound event of grid view.
Register event on page load
GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[3].BackColor = System.Drawing.Color.FromName("red");
}
}
Please refer
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
hope this will help you.