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