| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1181 人关注过本帖
标题:DataGridView 禁止 button 问题
只看楼主 加入收藏
brucewillis
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-11-30
收藏
 问题点数:0 回复次数:2 
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
sliverlight
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2014-12-1
收藏
得分:0 
你是做AE组件二次开发么
2014-12-01 12:28
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:0 
你自己定义的类型button,我没看你代码,我的第一反应时给GridView的单元格设置单元格的形式【我是这样叫的】,就是设置它是文本还是按钮。设置好了你在转换
图片附件: 游客没有浏览图片的权限,请 登录注册

Maybe
2014-12-01 17:10
快速回复:DataGridView 禁止 button 问题
数据加载中...
 
   



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

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