到面前爲止,net裏的DataGridView控件不能實現你的要求,如下:
{
if (e.ColumnIndex == 2 && e.RowIndex != -1) //判斷所在列
{
DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
int a = Convert.ToInt32(cell.Value);
if (a > 10) //判斷變色條件
{
//這裏把整行的背景色都變了。
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.BurlyWood;
}
}
}
//想必上面的代碼你很清楚,DataGridView控件無法實現你的需求,兩個解決辦法:
//1.自己擴展DataGridView控件,使之實現你的要求.
//2.找第三方控件.這裏我用過DevExpress的GridControl控件,功能非常強大.完全可以實現這個功能,它可以直接設置每個單元格的外觀.
//註冊RowStyleChange事件
private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
GridView view = sender as GridView;
if (e.RowHandle >= 0)
{
string category = view.GetRowCellDisplayText(e.RowHandle, view.Columns["Category"]);
if (category == "Beverages")
{
e.Appearance.BackColor = Color.Salmon;
e.Appearance.BackColor2 = Color.SeaShell;
}
}
}