有没有简单的方法来禁用/灰显DataGridView?例如,在做
时dgv.Enabled = false
dgv的外观不会改变。我看到有人追加以下内容:
dgv.forecolor = gray
dgv.columnheader.forecolor = gray
但是,这看起来很笨拙。还有更好的方法吗?
答案 0 :(得分:19)
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
答案 1 :(得分:15)
简单回答你的问题:不,没有更好的方法。
MSDN对此主题大多保持沉默,但论坛却充满了热情。手动将背景颜色设置为灰色是大多数人在DGV上“禁用”外观的方式。
答案 2 :(得分:4)
sveilleux2的示例,仅在C#(标记)和高级(允许您将其放在任何名称和任意数量的DataGridViews上)
def execute():
p = subprocess.Popen(params)
return (p)
答案 3 :(得分:3)
为标题设置灰色不会改变它。您还需要将EnableHeadersVisualStyles切换为false。
dgv.ForeColor = Color.Gray;
dgv.ColumnHeadersDefaultCellStyle.ForeColor = Color.Gray;
dgv.EnableHeadersVisualStyles = false;
答案 4 :(得分:1)
我知道这是一个已经解决的问题但希望防止其他人失去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;
}
}
}
答案 5 :(得分:0)
我假设您希望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
}
希望这会有所帮助。 :
答案 6 :(得分:0)
即使这个问题有点老,我也会在这里添加它 - 我通过覆盖控件上的Paint
方法来绘制透明框,这样做与其他方法不同。我使用了从基类DataGridView
继承的类,然后为OnPaint
方法提供了一些额外的属性和覆盖。您也许可以在Paint
事件中执行此操作,但对我来说,我已经创建了自己的控件版本。
这样做的好处是不会更改您已设置的任何行/单元格颜色/格式,只是想在禁用它时调暗控件。
只需设置DisableColor
(例如Black)使其变暗(您也可以使用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);
}
}
}
答案 7 :(得分:-1)
设置ReadOnly = false会改变外观吗?我认为可能会使数据报文的“可点击”部分变灰,例如列标题。但您仍然可以看到其中的数据。