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

           

        }

阅读更多
个人分类: .Net 2.0
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭