Подтвердить что ты не робот

Отключение или выделение DataGridView

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

dgv.Enabled = false

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

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

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

4b9b3361

Ответ 1

Простой ответ на ваш вопрос: нет, нет лучшего способа.

MSDN в основном молчат по теме, но форумы ошеломляют. Ручная настройка цвета фона на серый - это то, как большинство людей получают "отключенный" вид DGV.

Ответ 2

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

Ответ 3

Просто установка серого цвета для заголовка не изменит его. Вам также необходимо переключить EnableHeadersVisualStyles на false.

dgv.ForeColor = Color.Gray;
dgv.ColumnHeadersDefaultCellStyle.ForeColor = Color.Gray;
dgv.EnableHeadersVisualStyles = false;

Ответ 4

Я понимаю, что это решение, но нужно предотвратить потерю 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

действительно ли установка ReadOnly = false изменяет внешний вид? Я подумал, что возможно, что greyed из "clickable" частей datagraid, таких как заголовки столбцов... но вы все еще можете видеть данные в нем.

Ответ 6

Я предполагаю, что вы хотите, чтобы 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
  }

Надеюсь, это поможет.:]

Ответ 7

Пример sveilleux2, только в С# (который является тегом) и расширенный (позволяет разместить его на любом имени и на любом количестве 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;
        }
    }