datagridview单元格重绘

问题:最近有个需求,要将设置好的参数放到单元格里,分为两列显示,然后考虑用重绘单元格的方法实现。

实现思路:将所有设置的参数组合成一条字符串,参数之间用分号“;”分隔开,然后将参数字符串设为单元格的Value上,这样会触发单元格的CellPainting事件,在CellPainting事件中,将字符串拆分,分别进行重绘(自己控制字符串位置)。(可以参考MSDN中的DataGridView.CellPainting 事件)

code:  

private void dgvProtocol_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == dgvProtocol.Columns["colProtocolParam"].Index && e.RowIndex >= 0)
            {
                if (string.IsNullOrEmpty(e.Value.ToString()))
                    return;

                    
                //分割参数字符串
                string[] valueArry = e.Value.ToString().Split(new Char[] { ';' });
                List
   
   
    
     valueList = valueArry.ToList();
                valueList.Remove("");


                Brush gridBrush = new SolidBrush(this.dgvProtocol.GridColor);//线条画刷
                Brush backColorBrush = null;//背景画刷,若单元格被选中,则以选中是的背景色创建画刷,否则,以背景色创建画刷
                if (dgvProtocol.Rows[e.RowIndex].Selected)
                {
                    backColorBrush = new SolidBrush(e.CellStyle.SelectionBackColor);
                }
                else
                {
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor);
                }

                Pen gridLinePen = new Pen(gridBrush);//线条画笔

                // Erase the cell.
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);//重绘单元格背景

                // Draw the grid lines (only the right and bottom lines;
                // DataGridView takes care of the others).
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                    e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                    e.CellBounds.Bottom - 1);
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                    e.CellBounds.Top, e.CellBounds.Right - 1,
                    e.CellBounds.Bottom);

                // Draw the text content of the cell, ignoring alignment.

                //int row = valueList.Count % 2 == 0 ? valueList.Count / 2 : valueList.Count / 2 + 1;
                const int maxRow = 5;//最多5行(自定义的单元格内最多放的行数)
                int height = e.CellBounds.Height / maxRow;//每行的高度
                int width = e.CellBounds.Width / 2;//单元格一半宽度,因为现在是要分成两列来显示

                Brush fontBrush = new SolidBrush(dgvProtocol.ForeColor);//字体画刷,控制字体颜色
                for (int i = 0; i < valueList.Count; i++)
                {
                    if (i % 2 == 0)//画第一列字符串
                    {
                        e.Graphics.DrawString(valueList[i], e.CellStyle.Font,
                                fontBrush, e.CellBounds.X + 2,
                                e.CellBounds.Y + height * (i / 2), StringFormat.GenericDefault);
                    }
                    else//画第二列字符串
                    {
                        e.Graphics.DrawString(valueList[i], e.CellStyle.Font,
                                                        fontBrush, e.CellBounds.X + width,
                                                        e.CellBounds.Y + height * (i / 2), StringFormat.GenericDefault);
                    }

                }                   
            
                e.Handled = true;
            }
        }

   
   

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值