| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1182 人关注过本帖
标题:DataGridView 禁止 button 问题
取消只看楼主 加入收藏
brucewillis
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-11-30
收藏
 问题点数:0 回复次数:0 
DataGridView 禁止 button 问题
我参考使用的MSDN的方法:http://msdn.(VS.80).aspx
但是运行没有效果,检查发现问题原因是无法强制转化datagridview的cell为自定义的DisableButton,请问该如何解决?
问题描述:        (DataGridViewDisableButtonCell)dataGridView1.Rows[i].Cells["ButtonTest"]    Cannot cast 'dataGridView1.Rows[i].Cells["ButtonTest"]' (which has an actual type of 'System.Windows.Forms.DataGridViewButtonCell') to 'WinFormsApp.Form1.DataGridViewDisableButtonCell'    WinFormsApp.Form1.DataGridViewDisableButtonCell
代码:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'myTestDataSet.persons' table. You can move, or remove it, as needed.
            this.personsTableAdapter.Fill(this.myTestDataSet.persons);
            DataGridViewDisableButtonCell buttenCell = new DataGridViewDisableButtonCell();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells["isManagerDataGridViewCheckBoxColumn"].Value == DBNull.Value)
                {
                    dataGridView1.Rows[i].Cells["isManagerDataGridViewCheckBoxColumn"].Value = 0;
                }

                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["isManagerDataGridViewCheckBoxColumn"].Value) == false)
                {
                    buttenCell = (DataGridViewDisableButtonCell)dataGridView1.Rows[i].Cells["ButtonTest"];
                    buttenCell.Enabled = false;
                }
            }
            
        }

        public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn
        {
            public DataGridViewDisableButtonColumn()
            {
                this.CellTemplate = new DataGridViewDisableButtonCell();
            }
        }

        public class DataGridViewDisableButtonCell : DataGridViewButtonCell
        {
            private bool enabledValue;
            public bool Enabled
            {
                get
                {
                    return enabledValue;
                }
                set
                {
                    enabledValue = value;
                }
            }

            public override object Clone()
            {
                DataGridViewDisableButtonCell cell =
                    (DataGridViewDisableButtonCell)base.Clone();
                cell.Enabled = this.Enabled;
                return cell;
            }

            public DataGridViewDisableButtonCell()
            {
                this.enabledValue = true;
            }

            protected override void Paint(Graphics graphics,
                Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
                DataGridViewElementStates elementState, object value,
                object formattedValue, string errorText,
                DataGridViewCellStyle cellStyle,
                DataGridViewAdvancedBorderStyle advancedBorderStyle,
                DataGridViewPaintParts paintParts)
            {
                if (!this.enabledValue)
                {
                    if ((paintParts & DataGridViewPaintParts.Background) ==
                        DataGridViewPaintParts.Background)
                    {
                        SolidBrush cellBackground =
                            new SolidBrush(cellStyle.BackColor);
                        graphics.FillRectangle(cellBackground, cellBounds);
                        cellBackground.Dispose();
                    }

                    if ((paintParts & DataGridViewPaintParts.Border) ==
                        DataGridViewPaintParts.Border)
                    {
                        PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
                            advancedBorderStyle);
                    }
                    Rectangle buttonArea = cellBounds;
                    Rectangle buttonAdjustment =
                        this.BorderWidths(advancedBorderStyle);
                    buttonArea.X += buttonAdjustment.X;
                    buttonArea.Y += buttonAdjustment.Y;
                    buttonArea.Height -= buttonAdjustment.Height;
                    buttonArea.Width -= buttonAdjustment.Width;
                    ButtonRenderer.DrawButton(graphics, buttonArea,
                        System.Windows.Forms.VisualStyles.PushButtonState.Disabled);

                    if (this.FormattedValue is String)
                    {
                        TextRenderer.DrawText(graphics,
                            (string)this.FormattedValue,
                            this.DataGridView.Font,
                            buttonArea, SystemColors.GrayText);
                    }
                }
                else
                {
                    base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                        elementState, value, formattedValue, errorText,
                        cellStyle, advancedBorderStyle, paintParts);
                }
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            MessageBox.Show("Button clicked");
        }  
    }
}
搜索更多相关主题的帖子: button actual 如何 
2014-11-30 10:44
快速回复:DataGridView 禁止 button 问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.092990 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved