我有一个包含 4 列的数据网格视图,前 2 列是组合框列,第三列是文本框列,第四列是按钮列。在表单加载中,我必须禁用数据网格的整个按钮列,在此之后我应该选择前三列保存后,将前三列保存在数据库中,特定行中的按钮列应启用。前三列应通过单击按钮保存在数据库中。 请帮助我,我已经被这个问题困扰很多天了 这是我使用的代码
private void SATAddTemplate_Load(object sender, EventArgs e)
{
foreach (DataGridViewRow row in datagrdADDTEMP.Rows)
{
DataGridViewButtonCell btn = (DataGridViewButtonCell)row.Cells[3];
btn.ReadOnly = true;
}
}
private void btnSaveSettings_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in datagrdADDTEMP.Rows)
{
DataGridViewButtonCell btn = (DataGridViewButtonCell)row.Cells[3];
btn.ReadOnly = false;
}
}
这里有一些关于设置
Enabled
中出现的按钮的 DataGridViewButtonColumn
属性问题的帮助。
您需要扩展
DataGridViewButtonColumn
来创建您自己的带有可禁用按钮的 DataGridView 列。 MSDN 上的这篇文章 详细介绍了如何执行此操作。
本文有很多代码,我鼓励您仔细查看,但您真正需要做的就是将文章中找到的以下类复制并粘贴到您的项目中:
-- DataGridViewDisableButtonColumn
-- DataGridViewDisableButtonCell
完成此操作后,您将能够将
DataGridViewDisableButtonColumn
添加到 DataGridView 中。使用自定义列中公开的公共 Enabled
属性来设置每个单元格按钮控件的 Enabled
属性。由于您想要设置列中所有按钮的 Enabled
属性,您可以编写一个辅助方法,循环遍历 DataGridView 中的所有行并设置 Enabled
属性:
private void SetDGVButtonColumnEnable(bool enabled) {
foreach (DataGridViewRow row in dataGridView1.Rows) {
// Set Enabled property of the fourth column in the DGV.
((DataGridViewDisableButtonCell)row.Cells[3]).Enabled = enabled;
}
dataGridView1.Refresh();
}
这是对Jay答案的补充。
根据请求,这是我用来创建可以禁用的按钮单元的代码。它包括双缓冲,以便用户滚动时按钮不会闪烁。
/// <summary>
/// Adapted from https://msdn.microsoft.com/en-us/library/ms171619.aspx. Double-buffering was added to remove flicker on re-paints.
/// </summary>
public class DataGridViewDisableButtonCell : DataGridViewButtonCell
{
private bool enabledValue;
public bool Enabled
{
get { return enabledValue; }
set
{
if (enabledValue == value) return;
enabledValue = value;
// force the cell to be re-painted
if (DataGridView != null) DataGridView.InvalidateCell(this);
}
}
// Override the Clone method so that the Enabled property is copied.
public override object Clone()
{
var cell = (DataGridViewDisableButtonCell) base.Clone();
cell.Enabled = Enabled;
return cell;
}
// By default, enable the button cell.
public DataGridViewDisableButtonCell()
{
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 (!enabledValue)
{
var currentContext = BufferedGraphicsManager.Current;
using (var myBuffer = currentContext.Allocate(graphics, cellBounds))
{
// Draw the cell background, if specified.
if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
{
using (var cellBackground = new SolidBrush(cellStyle.BackColor))
{
myBuffer.Graphics.FillRectangle(cellBackground, cellBounds);
}
}
// Draw the cell borders, if specified.
if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
{
PaintBorder(myBuffer.Graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
}
// Calculate the area in which to draw the button.
var buttonArea = cellBounds;
var buttonAdjustment = BorderWidths(advancedBorderStyle);
buttonArea.X += buttonAdjustment.X;
buttonArea.Y += buttonAdjustment.Y;
buttonArea.Height -= buttonAdjustment.Height;
buttonArea.Width -= buttonAdjustment.Width;
// Draw the disabled button.
ButtonRenderer.DrawButton(myBuffer.Graphics, buttonArea, PushButtonState.Disabled);
// Draw the disabled button text.
var formattedValueString = FormattedValue as string;
if (formattedValueString != null)
{
TextRenderer.DrawText(myBuffer.Graphics, formattedValueString, DataGridView.Font, buttonArea, SystemColors.GrayText, TextFormatFlags.PreserveGraphicsTranslateTransform | TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
}
myBuffer.Render();
}
}
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);
}
}
}
您可以使用这篇 MSDN 文章 MSDN 文章:在 dataGridView 中禁用按钮 它使用 datagridview 按钮的类,请注意,只要您愿意处理按钮,就必须检查按钮的启用状态
我对@Chris Staley 的答案进行了两项调整(因此对@Jay Riggs 的答案进行了调整)。
1: 如果
SelectionMode
是DataGridView的FullRowSelect
,您可以添加以下代码来显示按钮单元格中所选行的突出显示:
// Draw the cell background, if specified.
if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
{
var backColor = elementState.HasFlag(DataGridViewElementStates.Selected)
? cellStyle.SelectionBackColor
: cellStyle.BackColor;
using var cellBackground = new SolidBrush(backColor);
myBuffer.Graphics.FillRectangle(cellBackground, cellBounds);
}
2: 为了正确定义边框,使其与“正常”按钮单元格匹配(以便按钮不会填充整个单元格),您可以添加以下代码:
// Calculate the area in which to draw the button.
var buttonArea = cellBounds;
var cellPadding = cellStyle.Padding;
var buttonAdjustment = BorderWidths(advancedBorderStyle);
buttonArea.X += cellPadding.Left + buttonAdjustment.Left;
buttonArea.Y += cellPadding.Top + buttonAdjustment.Top;
buttonArea.Width -= cellPadding.Horizontal + buttonAdjustment.Left + buttonAdjustment.Right;
buttonArea.Height -= cellPadding.Vertical + buttonAdjustment.Top + buttonAdjustment.Bottom;