开发者社区> 余二五> 正文

DataGridView的CellFormatting事件和CellPainting事件

简介:
+关注继续查看
1CellFormatting事件,一般重绘单元格属性。
    private Bitmap highPriImage;
    private Bitmap mediumPriImage;
    private Bitmap lowPriImage;
private void dataGridView1_CellFormatting(object sender, 
        System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
    {
        // Set the background to red for negative values in the Balance column.
        if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
        {
            Int32 intValue;
            if (Int32.TryParse((String)e.Value, out intValue) && 
                (intValue < 0))
            {
                e.CellStyle.BackColor = Color.Red;
                e.CellStyle.SelectionBackColor = Color.DarkRed;
            }
        }

        // Replace string values in the Priority column with images.
        if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Priority"))
        {
            // Ensure that the value is a string.
            String stringValue = e.Value as string;
            if (stringValue == null) return;

            // Set the cell ToolTip to the text value.
            DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
            cell.ToolTipText = stringValue;

            // Replace the string value with the image value.
            switch (stringValue)
            {
                case "high":
                    e.Value = highPriImage;
                    break;
                case "medium":
                    e.Value = mediumPriImage;
                    break;
                case "low":
                    e.Value = lowPriImage;
                    break;
            }
        }
    }


2
CellPainting事件,一般用于合并单元格用
Windows Forms DataGridView 
没有提供合并单元格的功能,要实现合并单元格的功能就要在CellPainting事件中使用Graphics.DrawLine Graphics.DrawString 自己来
下面的代码可以对DataGridView1列内容相同的单元格进行合并:
 #region"
合并单元格的测试"
private int? nextrow = null;
private int? nextcol = null;
private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
    if (this.dataGridView1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
    {
        if (this.nextcol != null & e.ColumnIndex == this.nextcol)
        {
            e.CellStyle.BackColor = Color.LightBlue;
            this.nextcol = null;
        }
        if (this.nextrow != null & e.RowIndex == nextrow)
        {
            e.CellStyle.BackColor = Color.LightPink;
            this.nextrow = null;
        }
        if (e.RowIndex != this.dataGridView1.RowCount - 1)
        {
            if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
            {
                e.CellStyle.BackColor = Color.LightPink;
                nextrow = e.RowIndex + 1;
            }
        }
    }
    if (this.dataGridView1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)
    {
        if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
        {
            e.CellStyle.BackColor = Color.LightBlue;
            nextcol = e.ColumnIndex + 1;
        }
    }
}
//==========================
       
//
绘制单元格

private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{
 
    //纵向合并
    if (this.dataGridView1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
    {
        using (
            Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
            backColorBrush = new SolidBrush(e.CellStyle.BackColor))
        {
            using (Pen gridLinePen = new Pen(gridBrush))
            {
                // 
擦除原单元格背景

                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                ////
绘制线条,这些线条是单元格相互间隔的区分线条,
                ////
因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条

                if (e.RowIndex != this.dataGridView1.RowCount - 1)
                {
                    if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
                    {
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//
下边缘的线

                        //
绘制值
                        if (e.Value != null)
                        {
                            e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                Brushes.Crimson, e.CellBounds.X + 2,
                                e.CellBounds.Y + 2, StringFormat.GenericDefault);
                        }
                    }
                }
                else
                {
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//
下边缘的线
                    //
绘制值
                    if (e.Value != null)
                    {
                        e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                            Brushes.Crimson, e.CellBounds.X + 2,
                            e.CellBounds.Y + 2, StringFormat.GenericDefault);
                    }
                }
                //
右侧的线
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                    e.CellBounds.Top, e.CellBounds.Right - 1,
                    e.CellBounds.Bottom - 1);
                e.Handled = true;
            }
        }
    }
    //横向合并
    if (this.dataGridView1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)
    {
        using (
            Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
            backColorBrush = new SolidBrush(e.CellStyle.BackColor))
        {
            using (Pen gridLinePen = new Pen(gridBrush))
            {
                // 
擦除原单元格背景

                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
                {
                    //右侧的线
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                    //
绘制值
                    if (e.Value != null)
                    {
                        e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                            Brushes.Crimson, e.CellBounds.X + 2,
                            e.CellBounds.Y + 2, StringFormat.GenericDefault);
                    }
                }
                //下边缘的线
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                            e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                e.Handled = true;
            }
        }
    }
}
#endregion




本文转自 qianshao 51CTO博客,原文链接:http://blog.51cto.com/qianshao/201801,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
关于DataGridView_DataError事件的问题
本文转载:http://blog.csdn.net/szstephenzhou/article/details/7834725  --关于DataGridView_DataError事件的问题      /******************************************...
849 0
gridview自定义button事件 ,无法触发 onrowcommand
如题。 原因:按钮button有回传事件,当点击button,页面回传,已经将原来的页面的事件冲掉了。   解决方法: 1方法:  Page_Load方法里,添加if(!ispostback){//绑定gridview的值}   2方法:去掉button的回传事件,添加一个属性:UseS...
675 0
DropDownlist数据SelectedIndexChanged触发问题解决
1、设置DropDownlist的AutoPostBack为True 2、绑定DropDownlist数据时出现了重复项, 在载入数据时保存数据状态应该写在Load事件中的if (!IsPostBack) 条件下 3、必须保证页面只有一个runat="server"的表单 4、页面中有一些按钮命名上用到Submit,替换所有的Submit重命名才可以提交表单,事件就可以触发到后台 5、 中 不能是 ViewStateMode="Disabled"   我出现的问题是第5条,刷新一下,原来的数据没了,真是问题太多。
965 0
TabControl的SelectionChanged事件
DataGrid作为TabControl控件的TabItem的content元素。 当操作DataGrid的不同cell时,会引发了TabControl的SelectionChanged事件的问题。 正确的使用方式有2中方法: 方法一: private void TabControl_Sel...
1001 0
datagridview设置某一列可写
    实践证明,在winform的窗体设计时,将datagridview.readonly=false;     或者在datagridview Edit Columns时设置某一列readonly=false都是没起多大作用的。
731 0
给DataGrid单元行添加双击事件
现在我需要做到的功能是当我单击DataGrid某行时显示相对应选中的数据信息,在双击此相同行时弹出删除对话框,应该怎么做呢。由于单击问题很简单就不再阐述了,下面我说一下双击事件是怎么实现的。       这里用到了DataGrid的ItemDataBound事件,我们可以把下面的代码加入到所需的程序中就可实现双击的功能。
739 0
kendo ui grid选中行事件,获取combobox选择的值
背景:   以前用 telerik ui做的grid现在又要换成kendo ui,不过说句实话kendo ui真的比telerik好多,可以说超级升级改头换面。当然用的mvc的辅助方法,以前的telerik ui 选中行的事件是 .OnRowSelect("SelectRow”) ,换成kendo ui 选中行的事件是   .Events(p=>p.Change("SelectRow")) 实现: kendo ui  grid 选中行的事件是  .Events(p=>p.Change("SelectRow"))  。
1800 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载