我用VB.NET做的,你自己转C#的吧,给你举个例子首先DataGridView1.EnableHeadersVisualStyles = false; 然后在DataGridView的CellPainting事件中加以下代码If e.RowIndex >= 0 Then 'Button是那一例的Name If e.ColumnIndex = DataGridView1.Columns("Button").Index Then '先画一个背景,建议你自己做一个按钮的图片,在这里画上那个图片,如果像我这么做的话,相当难看. e.Graphics.FillRectangle(Brushes.Blue, e.CellBounds) '再画字,字的位置可以用e.CellBounds中的X,Y来判断一下. e.Graphics.DrawString(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value, e.CellStyle.Font, Brushes.Red, e.CellBounds.X + 1, e.CellBounds.Y + 1) e.Handled = True End If End If
在你的grid的RowDataBound事件里增加,第六列为Honeydew背景色e.Row.Cells[6].BackColor = System.Drawing.Color.Honeydew;
CssClass="自己定义的样式名"
是winForm程序。
e.Row.Cells[6].BackColor = System.Drawing.Color.Honeydew;
这样是没用的按钮还是默认颜色。
然后在DataGridView的CellPainting事件中加以下代码If e.RowIndex >= 0 Then
'Button是那一例的Name
If e.ColumnIndex = DataGridView1.Columns("Button").Index Then
'先画一个背景,建议你自己做一个按钮的图片,在这里画上那个图片,如果像我这么做的话,相当难看.
e.Graphics.FillRectangle(Brushes.Blue, e.CellBounds)
'再画字,字的位置可以用e.CellBounds中的X,Y来判断一下.
e.Graphics.DrawString(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value, e.CellStyle.Font, Brushes.Red, e.CellBounds.X + 1, e.CellBounds.Y + 1)
e.Handled = True
End If
End If
#region private void SetDataGridViewButton()设置DataGridView中按钮背景和字体颜色
/// <summary>
/// 设置DataGridView中按钮背景和字体颜色
/// </summary>
private void SetDataGridViewButton()
{
///SelectButton是DataGridView中的列名可自定义,就是你定义Button的名称。
//背景颜色
this.SelectButton.DefaultCellStyle.BackColor = Color.Beige;
//字体颜色
this.SelectButton.DefaultCellStyle.ForeColor = Color.Red;
}
#endregion
和DataGridView1.EnableHeadersVisualStyles = false;没有关系,不用加它
在c# 中datagridview控件的按钮列,要变按钮的颜色设置backcolor只有在你的桌面主题是windows经典样式的时候才有作用,把主题换成windows XP样式后按钮就又变成了windows默认的灰白表面,
现在我在该列的FLATSTYLE属性里面设置为POPUP后就可以了,虽然难看点但是姑且达到要求了(原来默认为STANDARD).
在此谢谢各位分就大家平均吧!