как ссылаться на несвязанное поле со списком в DataGridView - PullRequest
1 голос
/ 05 марта 2011

Я использую событие CellClick и хочу обновить еще один флажок в сетке.Пример: -Два столбца с Добавить и Удалить столбец.Пользователь нажимает Добавить, и система проверяет, что флажок удаления также не установлен.если выбрано «Удалить» ------ установите «Удалить» на «ложь»

Другими словами, флажки «Добавить» и «Удалить» для одной и той же строки не должны быть оба отмечены.

Я использую...

private void customersDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
  1. как получить значение текущей ячейки по имени «Добавить».
  2. как получить значение другой ячейки по имени «Удалить»».Вышеописанный процесс является триггером / флопом.

Если я узнаю, как получить доступ к ячейке как к объекту, я смогу сделать все остальное.

Я продолжаю находить пример, в котором используетсяcmbBox = e.Control as ComboBox но это не работает: (

Ссылки на примеры помогут вам.


Добавлено из предложенного вами изменения в ответ -Andomar

Это работает ...

private void customersDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
    {   
        //Set a var that determined whether or not the checkbox is selected       
        bool selected = (bool)this.customersDataGridView[e.ColumnIndex, e.RowIndex].Selected;
        //Do the flip-flop here  
        const int add = 4;
        const int delete = 5;
        switch (e.ColumnIndex)
        {
            //If the checkbox in the Add column changed,
            //  flip the value of the corresponding Delete column           
            case add:
                this.customersDataGridView[delete, e.RowIndex].Value = !selected;
                break;
            //If the checkbox in the Delete column changed, 
            //  flop the value of the corresponding Add column       
            case delete:
                this.customersDataGridView[add, e.RowIndex].Value = !selected;
                break;
        }
    }
}

нет необходимости в dataGridView1_CellMouseUp

1 Ответ

2 голосов
/ 05 марта 2011

Попробуйте использовать событие CellValueChanged вместо события CellClick.Приведенный ниже обработчик установит значение флажка в соответствии с противоположным значением его флажка аналога (т. Е. Флажков, находящихся в одной строке).

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
    {
        //Set a var that determined whether or not the checkbox is selected
        bool selected = (bool)this.dataGridView1[e.ColumnIndex, e.RowIndex].Value;

        //Do the flip-flop here
        switch (e.ColumnIndex)
        {
            //If the checkbox in the Add column changed, flip the value of the corresponding Delete column
            case 0:
                this.dataGridView1[1, e.RowIndex].Value = !selected;
                break;
            //If the checkbox in the Delete column changed, flop the value of the corresponding Add column
            case 1:
                this.dataGridView1[0, e.RowIndex].Value = !selected;
                break;
        }
    }
}

//You may need to do something goofy like this to update the DataGrid 
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{

    if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
    {
        this.dataGridView1.EndEdit();
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...