Отключение или выделение серого цвета DataGridView

Есть ли простой способ отключить / затенять DataGridView? Например, при выполнении

dgv.Enabled = false

Внешний вид dgv не меняется. Я видел, как люди добавляли следующее:

dgv.forecolor = gray
dgv.columnheader.forecolor = gray

Однако это кажется неуклюжим. Есть способ лучше?

22
задан Jeb 3 January 2012 в 16:39
поделиться

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

}

1
ответ дан 29 November 2019 в 04:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: