我有以下代码:
private void dgvStatus_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dgvStatus.Rows)
{
row.Cells[color.Index].Style.BackColor = Color.FromArgb(((GesTest.dsEssais.FMstatusAnomalieRow)row.DataBoundItem).iColor);
}
}
我正在尝试从背景颜色列设置每个单元格的背景颜色。这不起作用颜色永远不会改变。知道为什么吗?
我一直在四处寻找,但没有找到任何有用的东西
答案 0 :(得分:27)
只需创建一个新的DataGridViewCellStyle对象,设置其背景颜色,然后将单元格的样式指定给它:
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.BackColor = Color.FromArgb(((GesTest.dsEssais.FMstatusAnomalieRow)row.DataBoundItem).iColor);
style.ForeColor = Color.Black;
row.Cells[color.Index].Style = style;
答案 1 :(得分:15)
我终于成功了。代码如下:
private void dgvStatus_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex != color.Index)
return;
e.CellStyle.BackColor = Color.FromArgb(int.Parse(((DataRowView)dgvStatus.Rows[e.RowIndex].DataBoundItem).Row[4].ToString()));
}
如果有人知道这样做更好,请不要犹豫发布。我愿意接受建议
答案 2 :(得分:9)
如果你仍然对为什么感兴趣,这对你起初并不适用:
您没有看到对单元格样式所做的更改的原因是您在显示表单之前进行了更改,因此忽略了它们。
在此处建议的事件中更改单元格样式将完成这项工作,但它们会多次调用,导致样式更改发生的次数超出您的预期,因此效率不高。
要解决此问题,请在显示表单的代码中的点之后更改样式,或者订阅Shown事件,并将更改放在那里(这个事件的调用明显少于建议的其他事件)
答案 3 :(得分:4)
array_shift
答案 4 :(得分:2)
int rowscount = dataGridView1.Rows.Count;
for (int i = 0; i < rowscount; i++)
{
if (!(dataGridView1.Rows[i].Cells[8].Value == null))
{
dataGridView1.Rows[i].Cells[8].Style.BackColor = Color.LightGoldenrodYellow;
}
}
答案 5 :(得分:1)
类似于所示并提到:
注意事项:请注意,在DataGridView控件可见后,单元格将更改其颜色(仅)。 因此,一种可行的解决方案是使用:
VisibleChanged事件
如果您希望在创建新的Rows时保持样式;还订阅:
行收藏事件
以下示例:
///<summary> Instantiate the DataGridView Control. </summary>
private DataGridView dgView = new DataGridView;
///<summary> Method to configure DataGridView Control. </summary>
private void DataGridView_Configuration()
{
// In this case the method just contains the VisibleChanged event subscription.
dgView.VisibleChanged += DgView_VisibleChanged;
// Uncomment line bellow in case you want to keep the style when creating new rows.
// dgView.RowsAdded += DgView_RowsAdded;
}
///<summary> The actual Method that will re-design (Paint) DataGridView Cells. </summary>
private void DataGridView_PaintCells()
{
int nrRows = dgView.Rows.Count;
int nrColumns = dgView.Columns.Count;
Color green = Color.LimeGreen;
// Iterate over the total number of Rows
for (int row = 0; row < nrRows; row++)
{
// Iterate over the total number of Columns
for (int col = 0; col < nrColumns; col++)
{
// Paint cell location (column, row)
dgView[col, row].Style.BackColor = green;
}
}
}
///<summary> The DataGridView VisibleChanged Event. </summary>
private void DataGridView_VisibleChanged(object sender, EventArgs e)
{
DataGridView_PaintCells();
}
/// <summary> Occurrs when a new Row is Created. </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
DataGridView_PaintCells();
}
最后::只需调用DataGridView_Configuration()(方法)
即:表单加载事件。
答案 6 :(得分:0)
尝试以下(在GridView的RowDataBound方法中):
protected void GridViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
// this will only change the rows backgound not the column header
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].BackColor = System.Drawing.Color.LightCyan; //first col
e.Row.Cells[1].BackColor = System.Drawing.Color.Black; // second col
}
}
答案 7 :(得分:0)
protected void grdDataListeDetay_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[3].Text != "0")
{
for (int i = 0; i <= e.Row.Cells.Count - 1; i++)
{
e.Row.Cells[i].BackColor = System.Drawing.Color.Beige;
}
}
}
}
答案 8 :(得分:0)
dataGridView1[row, col].Style.BackColor = System.Drawing.Color.Red;
答案 9 :(得分:0)
您可以使用VisibleChanged
事件处理程序。
private void DataGridView1_VisibleChanged(object sender, System.EventArgs e)
{
var grid = sender as DataGridView;
grid.Rows[0].Cells[0].Style.BackColor = Color.Yellow;
}