.net 在C Windows应用程序中尝试禁用DataGrid列中的按钮时出错
我在尝试禁用datagird列中的按钮时遇到错误。 错误如下所示: [A] DataGridViewDisableButtonCell无法强制转换为[B]DataGridViewDisableButtonCell。类型A源自位置“E:\Projects\IPMSGatePassManagementSystem\IPMSGatePassManagementSystem\bin\Debug\IPMSGatePassManagementSystem.exe”上下文“Default”中的“IPMSGatePassManagementSystem,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”。类型B源自位置“E:\Projects\IPMSGatePassManagementSystem\IPMSGatePassManagementSystem\bin\Debug\IPMSGatePassManagementSystem.exe”上下文“Default”中的“IPMSGatePassManagementSystem,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null” 在此行中获取此错误:.net 在C Windows应用程序中尝试禁用DataGrid列中的按钮时出错,.net,c#-4.0,datagrid,.net,C# 4.0,Datagrid,我在尝试禁用datagird列中的按钮时遇到错误。 错误如下所示: [A] DataGridViewDisableButtonCell无法强制转换为[B]DataGridViewDisableButtonCell。类型A源自位置“E:\Projects\IPMSGatePassManagementSystem\IPMSGatePassManagementSystem\bin\Debug\IPMSGatePassManagementSystem.exe”上下文“Default”中的“IPMSGat
if (GatepassNo != "")
{
DataGridViewDisableButtonCell btncell = (DataGridViewDisableButtonCell)row.Cells["Action"]; //ERROR
//DataGridViewDisableButtonCell btncell = (DataGridViewDisableButtonCell)row.Cells["Action"];
btncell.Enabled = false;
btncell.ToolTipText = "Gate Pass Already Created !";
}
为了便于理解,请提供以下课程
#region DataGridView Disable Button(To Add Disable Button Column)
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;
}
}
// Override the Clone method so that the Enabled property is copied.
public override object Clone()
{
DataGridViewDisableButtonCell cell =
(DataGridViewDisableButtonCell)base.Clone();
cell.Enabled = this.Enabled;
return cell;
}
// By default, enable the button 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)
{
// The button cell is disabled, so paint the border,
// background, and disabled button for the cell.
if (!this.enabledValue)
{
// Draw the cell background, if specified.
if ((paintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background)
{
SolidBrush cellBackground =
new SolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
}
// Draw the cell borders, if specified.
if ((paintParts & DataGridViewPaintParts.Border) ==
DataGridViewPaintParts.Border)
{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
advancedBorderStyle);
}
// Calculate the area in which to draw the button.
Rectangle buttonArea = cellBounds;
Rectangle buttonAdjustment =
this.BorderWidths(advancedBorderStyle);
buttonArea.X += buttonAdjustment.X;
buttonArea.Y += buttonAdjustment.Y;
buttonArea.Height -= buttonAdjustment.Height;
buttonArea.Width -= buttonAdjustment.Width;
// Draw the disabled button.
ButtonRenderer.DrawButton(graphics, buttonArea,
PushButtonState.Disabled);
// Draw the disabled button text.
if (this.FormattedValue is String)
{
TextRenderer.DrawText(graphics,
(string)this.FormattedValue,
this.DataGridView.Font,
buttonArea, SystemColors.GrayText);
}
}
else
{
// The button cell is enabled, so let the base class
// handle the painting.
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
elementState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, paintParts);
}
}
}
#endregion
请指导我解决这个问题。提前感谢。在实例化DataGridViewDisableCell之前,首先需要实例化DataGridViewDisableColumn并为其命名。下面是关于如何禁用按钮单元格的示例代码
var data = dal.GetTempDataFromDatabase();
dataGridView1.DataSource = data.Tables[0];
DataGridViewDisableButtonColumn column1 = new DataGridViewDisableButtonColumn();
column1.Name = "Buttons"; //<-- have you defiened the name "Action in your code?
dataGridView1.Columns.Add(column1);
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells["Buttons"].Value = "Buttons " + i.ToString();
}
DataGridViewDisableButtonCell btnCell = (DataGridViewDisableButtonCell) dataGridView1.Rows[0].Cells["Buttons"];
btnCell.Enabled = false;