DataGridView 实现一列单元格 显示多操作方式


RT:


主要使用的方法:

        在CellPainting绘制操作方式

                                  

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "Op")
                {
                    Employee model = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as Employee;
                    if (model != null)
                    {
                        StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat;
                        sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Center;
                        sf.Trimming = StringTrimming.EllipsisCharacter;
                        e.PaintBackground(e.CellBounds, false);
                        int md = model.Age % 3;
                        string text = "删除";
                        if (model.Age % 3 == 1)
                        {
                            text = "修改";
                        }
                        switch (md)
                        {
                            case 1:
                                e.Graphics.DrawString(text, this.dataGridView1.Font, Brushes.Red, e.CellBounds, sf);
                                break;
                            case 2: 
                                string text2 = "删除";
                                if (model.Age % 3 == 2)
                                {
                                    text2 = "修改";
                                }
                                SizeF size = e.Graphics.MeasureString(text, this.dataGridView1.Font);

                                SizeF size2 = e.Graphics.MeasureString(text2, this.dataGridView1.Font);
                                float z = size.Width / (size.Width + size2.Width);
                                float z2 = size2.Width / (size.Width + size2.Width);
                                RectangleF rect1 = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * z, e.CellBounds.Height);
                                RectangleF rect2 = new RectangleF(rect1.Right, e.CellBounds.Top, e.CellBounds.Width * z2, e.CellBounds.Height);
                                e.Graphics.DrawString(text, this.dataGridView1.Font, Brushes.Red, rect1, sf);
                                e.Graphics.DrawString(text2, this.dataGridView1.Font, Brushes.Red, rect2, sf);
                                break;
                            default: 
                                e.Graphics.DrawString(text, this.dataGridView1.Font, Brushes.Red, e.CellBounds, sf);
                                break; 
                        } 
                    }
                    e.Handled = true;
                }
            }
        }

        在CellMouseClick 事件对动作进行处理。

                

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            Point pt = e.Location;
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "Op")
                {
                    Employee model = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as Employee;
                    if (model != null)
                    {
                        int md = model.Age % 3;
                        string text = "删除";
                        if (model.Age % 3 == 1)
                        {
                            text = "修改";
                        }
                        switch (md)
                        {
                            case 1:
                           
                                break;
                            case 2:
                                string text2 = "删除";
                                if (model.Age % 3 == 2)
                                {
                                    text2 = "修改";
                                }
                                Graphics g = this.dataGridView1.CreateGraphics();
                                SizeF size = g.MeasureString(text, this.dataGridView1.Font);

                                SizeF size2 = g.MeasureString(text2, this.dataGridView1.Font);
                                float z = size.Width / (size.Width + size2.Width);
                                float z2 = size2.Width / (size.Width + size2.Width);
                                Rectangle rect = new Rectangle(0, 0, this.dataGridView1.Columns[e.ColumnIndex].Width, this.dataGridView1.Rows[e.RowIndex].Height);
                                RectangleF rect1 = new RectangleF(rect.Left, rect.Top, rect.Width * z, rect.Height);
                                RectangleF rect2 = new RectangleF(rect1.Right, rect.Top, rect.Width * z2, rect.Height);
                                if (rect1.Contains(pt))
                                {
                                    MessageBox.Show("1111111");
                                }
                                if (rect2.Contains(pt))
                                {
                                    MessageBox.Show("2222222");
                                }
                                break;
                            default:
          
                                break;
                        } 
                    }
                }
            }
            
        }


上图:
             


源码:http://download.csdn.net/detail/zanfeng/9453608


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值