gridview单元格操作

来源:互联网 发布:雅思考试经验知乎 编辑:程序博客网 时间:2024/04/29 04:58

一       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);

                e.Appearance.ForeColor = Color.Red;
            }

        }

        /// <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;
}}




0 0
原创粉丝点击