禁用或使DataGridView变灰
本文关键字:DataGridView 变灰 | 更新日期: 2023-09-27 18:19:15
是否有任何简单的方法来禁用/灰色的DataGridView?例如,当
dgv.Enabled = false
dgv的外观没有变化。我看到有人附加了以下内容:
dgv.forecolor = gray
dgv.columnheader.forecolor = gray
然而,这看起来很笨拙。有没有更好的办法?
Private Sub DataGridView1_EnabledChanged(sender As Object, e As EventArgs) Handles DataGridView1.EnabledChanged
If Not DataGridView1.Enabled Then
DataGridView1.DefaultCellStyle.BackColor = SystemColors.Control
DataGridView1.DefaultCellStyle.ForeColor = SystemColors.GrayText
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control
DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.GrayText
DataGridView1.CurrentCell = Nothing
DataGridView1.ReadOnly = True
DataGridView1.EnableHeadersVisualStyles = False
Else
DataGridView1.DefaultCellStyle.BackColor = SystemColors.Window
DataGridView1.DefaultCellStyle.ForeColor = SystemColors.ControlText
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Window
DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlText
DataGridView1.ReadOnly = False
DataGridView1.EnableHeadersVisualStyles = True
End If
End Sub
对你的问题的简单回答:不,没有更好的办法。
MSDN对这个话题保持沉默,但论坛上议论纷纷。手动将背景色设置为灰色是大多数人在DGV上"残疾"的原因。
sveileux2的示例,仅在c#(这是标签)和高级(允许您将其放在任意名称和任意数量的DataGridViews上)
private void DataGridView_EnabledChanged(object sender, EventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (!dgv.Enabled) {
dgv.DefaultCellStyle.BackColor = SystemColors.Control;
dgv.DefaultCellStyle.ForeColor = SystemColors.GrayText;
dgv.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control;
dgv.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.GrayText;
dgv.CurrentCell = null;
dgv.ReadOnly = true;
dgv.EnableHeadersVisualStyles = false;
}
else {
dgv.DefaultCellStyle.BackColor = SystemColors.Window;
dgv.DefaultCellStyle.ForeColor = SystemColors.ControlText;
dgv.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Window;
dgv.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlText;
dgv.ReadOnly = false;
dgv.EnableHeadersVisualStyles = true;
}
}
我将在这里添加这个,即使这个问题有点老了-我通过重写控件上的Paint
方法来绘制透明框,这与其他方法不同。我使用了一个从基本DataGridView
继承的类,然后为OnPaint
方法提供了一些额外的属性和覆盖。您也可以在Paint
事件中这样做,但对我来说,我已经制作了我们自己的控件版本。
这样做的好处是不会改变您已经设置的任何行/单元格颜色/格式,只是想在控件被禁用时使其变暗。
简单地设置DisableColor
(例如黑色)使其变暗(你也可以用DisableColorAlpha
属性改变alpha通道)。否则,它会像往常一样运行。
/// <summary>
/// Color used when the grid is disabled
/// </summary>
[Category("Appearance"), DefaultValue(typeof(Color), "Transparent"), Description("Color to use when the control is disabled (should be transparent)")]
public Color DisableColor { get; set; }
/// <summary>
/// Color used when the grid is disabled
/// </summary>
[Category("Appearance"), DefaultValue(50), Description("Alpha channel value for disabled color (0-255)")]
public int DisableColorAlpha { get; set; }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.Enabled == false && DisableColor != Color.Transparent)
{
// paint a transparent box -- simulate disable
using (Brush b = new SolidBrush(Color.FromArgb(DisableColorAlpha, DisableColor)))
{
e.Graphics.FillRectangle(b, e.ClipRectangle);
}
}
}
设置标题颜色为灰色不会改变它。你还需要将EnableHeadersVisualStyles设置为false。
dgv.ForeColor = Color.Gray;
dgv.ColumnHeadersDefaultCellStyle.ForeColor = Color.Gray;
dgv.EnableHeadersVisualStyles = false;
我知道这是一个已经解决的问题,但我想防止为其他人损失1小时。
//C# version for buttons also. Inspired by sveilleux2.
private void DataGridView1_EnabledChanged(object sender, EventArgs e){
if (!DataGridView1.Enabled){
DataGridView1.DefaultCellStyle.BackColor = SystemColors.Control;
DataGridView1.DefaultCellStyle.ForeColor = SystemColors.GrayText;
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control;
DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.GrayText;
//Disable two colums of buttons
for (int i = 0; i < DataGridView1.RowCount; i++){
DataGridViewButtonCell buttonCell = (DataGridViewButtonCell)DataGridView1.Rows[i].Cells[1];
buttonCell.FlatStyle = FlatStyle.Popup;
buttonCell.Style.ForeColor = SystemColors.GrayText;
buttonCell.Style.BackColor = SystemColors.Control;
DataGridViewButtonCell buttonCell_2 = (DataGridViewButtonCell)DataGridView1.Rows[i].Cells[6];
buttonCell_2.FlatStyle = FlatStyle.Popup;
buttonCell_2.Style.ForeColor = SystemColors.GrayText;
buttonCell_2.Style.BackColor = SystemColors.Control;
}
DataGridView1.Columns[1].DefaultCellStyle.ForeColor = SystemColors.GrayText;
DataGridView1.Columns[1].DefaultCellStyle.BackColor = SystemColors.Control;
DataGridView1.ReadOnly = true;
DataGridView1.EnableHeadersVisualStyles = false;
DataGridView1.CurrentCell = null;
}else{
DataGridView1.DefaultCellStyle.BackColor = SystemColors.Window;
DataGridView1.DefaultCellStyle.ForeColor = SystemColors.ControlText;
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Window;
DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlText;
DataGridView1.ReadOnly = false;
DataGridView1.EnableHeadersVisualStyles = false;
//Enable two colums of buttons
for (int i = 0; i < DataGridView1.RowCount; i++){
DataGridViewButtonCell buttonCell = (DataGridViewButtonCell)DataGridView1.Rows[i].Cells[1];
buttonCell.FlatStyle = FlatStyle.Standard;
DataGridViewButtonCell buttonCell_2 = (DataGridViewButtonCell)DataGridView1.Rows[i].Cells[6];
buttonCell_2.FlatStyle = FlatStyle.Standard;
}
}
}
我假设您希望datagridview向用户显示信息,并拒绝用户以任何方式修改的能力。
private void IntializeDataGridView()
{
dataGridViewTest.ReadOnly = true;
// you can code permissions or colors as well
dataGridViewTest.AllowUserToAddRows = false;
dataGridViewTest.AllowUserToDeleteRows = false;
dataGridViewTest.AllowUserToOrderColumns = false;
dataGridViewTest.BackgroundColor = Color.LightGray;
//so on and so forth
}
希望这对你有帮助。:]
设置ReadOnly = false会改变外观吗?我认为可能是灰色的"可点击"部分的数据,如列标题..但你仍然可以看到其中的数据