一 DEV控件下的 gridview單元格操作
/// <summary>
/// 在指定單元格里面畫圓,寫文字,修改外觀(重繪數據時發生)/// </summary>
void gvBoardInfo_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
if (e.Column == gvBoardInfo.Columns["id"] && e.RowHandle > -1)
{
float x = e.Bounds.Location.X;
float y = e.Bounds.Location.Y;
e.Graphics.FillEllipse(Brushes.Red, x, y, 10, 10);
e.Graphics.DrawString(font, brush, x, y);
}
}
/// <summary>
/// 使單元格內容居中(重繪數據時發生)
/// </summary>
void gvBoardInfo_RowCellDefaultAlignment(object sender, DevExpress.XtraGrid.Views.Base.RowCellAlignmentEventArgs e)
{
e.HorzAlignment = HorzAlignment.Center;
}
/// <summary>
/// 修改指定單元格文字,背景等(重繪數據時發生)
/// </summary>
void gvBoardInfo_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
if (e.Column == gvBoardInfo.Columns["id"])
{
}
}
二 原生態 gridview 單元格操作
dgv.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//使內容居中
void
DGV_CellPainting(
object
sender, DataGridViewCellPaintingEventArgs e)
{
if
(e.RowIndex > -1 && e.ColumnIndex > -1)
{
e.PaintBackground(e.CellBounds,
false
);
e.Graphics.FillEllipse(Brushes.Red, e.CellBounds);
e.Graphics.DrawString(e.RowIndex.ToString() + e.ColumnIndex.ToString(),
this
.Font, Brushes.Black,
new
PointF(x,y));
e.Handled =
true
;
}}