WinForm开发中,如何disable DataGridView中的按钮

DataGridView比以前的DataGrid好用些了,但也有很多不足:
比如不能像以前一样将一个cell转换为button或textbox,然后对其设置属性。
这里有2种方法来disable一个DataGridView中的按钮。

目的:DataGridView中有一列是button。用来删除当前行的纪录。但不是所有记录都可以删除,需要根据数据源中CanDel列的值来决定,如果=0,则Delete button处于disable状态,否则Delete button 是Enabled。

1 自定义一个buttoncolumn类,继承DataGridViewButtonCell。具体请参考http://msdn2.microsoft.com/en-us/library/ms171619.aspx

 

2 在CellPainting事件中自己重画。听起来挺复杂,实际很简单。下面是我的代码:

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {

            //首先判断当前cell是不是delete button
            if (e.ColumnIndex == dataGridView1.Columns["Delete"].Index && e.RowIndex >= 0 && e.RowIndex < (dataGridView1.Rows.Count - 1))
            {

                //如果CanDel = 0,重绘按钮为disable状态
                if ((int)this._CVIS_MainDataSet.CollegeInfo.Rows[e.RowIndex]["CanDel"] == 0)
                {
                    Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
                    e.CellBounds.Y + 1, e.CellBounds.Width - 4,
                    e.CellBounds.Height - 4);
                    using ( Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
                            backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        //先抹去原来的cell背景
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                        using (Pen gridLinePen = new Pen(gridBrush))
                        {
                            // 画出上下两条边线,左右边线无需画
                            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);
                            // 计算button的大小
                            Rectangle buttonArea = e.CellBounds;
                            buttonArea.X += 1;
                            buttonArea.Y += 1;
                            buttonArea.Height -= 2;
                            buttonArea.Width -= 2;

                            // 画按钮
                            ButtonRenderer.DrawButton(e.Graphics, buttonArea,
                                System.Windows.Forms.VisualStyles.PushButtonState.Disabled);

                            // 画文字,用灰色表示disable状态

                            TextRenderer.DrawText(e.Graphics,
                                   (string)bt.Value,
                                    dataGridView1.Font,
                                    buttonArea, SystemColors.GrayText);

                        }
                    }

                   //别忘了这个
                    e.Handled = true;
                }
            }

           

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值