【问题标题】:how reference unbound combo box in a DataGridView如何在 DataGridView 中引用未绑定的组合框
【发布时间】:2011-03-05 11:01:37
【问题描述】:

我正在使用 CellClick 事件并想更新网格上的另一个复选框。 示例: - 添加和删除列的两列。 用户单击添加,系统检查删除复选框是否也未选中。 如果选中删除 ------ 将删除设置为 false

换句话说,不能同时选中同一行的“添加”和“删除”复选框。

我正在使用...

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

【问题讨论】:

  • 请注意,您可以 format lines as code 将它们缩进四个空格。编辑器工具栏中的“{}”按钮会为您执行此操作。您还可以通过在行前加上数字、句点和空格来格式化列表。编辑您的问题并尝试一下。单击编辑器工具栏中的橙色问号,了解有关使用 Markdown 进行格式化的更多信息和提示。

标签: c# winforms visual-studio-2008 datagridview


【解决方案1】:

尝试使用 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();
    }
}

【讨论】:

  • 只是提醒任何看到这个的人... CellClick 有效。
猜你喜欢
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-21
相关资源
最近更新 更多