Winform DataBinding - PullRequest
       5

Winform DataBinding

1 голос
/ 20 октября 2011

Мне нужно включить / отключить столбцы строк в сетке данных, это можно легко сделать путем зацикливания всех строк после его привязки.Но я хочу сделать это, пока данные привязаны ... есть ли способ сделать это?Также, как я могу включить / отключить ячейку строки?

dgvLayout.AutoGenerateColumns = false;
dgvLayout.DataSource = list;

в ячейке нажмите, но это не работает

if ((dgvLayout.Rows[e.RowIndex].Cells["colControlText"].Value.ToString()) == "-Invalid-")
{
    if (e.ColumnIndex == 2 || e.ColumnIndex == 5)
    {
        return;
    }
    else if (e.ColumnIndex == 1)
    {
        return;
    }
}

Ответы [ 2 ]

2 голосов
/ 21 октября 2011

вы можете использовать это решение для включения и отключения ячейки

Чтобы «отключить» ячейку, она должна быть доступна только для чтения и как-то неактивнаЭта функция включает / отключает DataGridViewCell:

    /// <summary>
    /// Toggles the "enabled" status of a cell in a DataGridView. There is no native
    /// support for disabling a cell, hence the need for this method. The disabled state
    /// means that the cell is read-only and grayed out.
    /// </summary>
    /// <param name="dc">Cell to enable/disable</param>
    /// <param name="enabled">Whether the cell is enabled or disabled</param>
    private void enableCell(DataGridViewCell dc, bool enabled) {
        //toggle read-only state
        dc.ReadOnly = !enabled;
        if (enabled)
        {
            //restore cell style to the default value
            dc.Style.BackColor = dc.OwningColumn.DefaultCellStyle.BackColor;
            dc.Style.ForeColor = dc.OwningColumn.DefaultCellStyle.ForeColor;
        }
        else { 
            //gray out the cell
            dc.Style.BackColor = Color.LightGray;
            dc.Style.ForeColor = Color.DarkGray;
        }
    }
2 голосов
/ 20 октября 2011

Вы можете написать свой код в событии RowsAdded таблицы данных

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...