代码之家  ›  专栏  ›  技术社区  ›  adopilot

DataGridView复选框选择

  •  2
  • adopilot  · 技术社区  · 15 年前

    我补充说 datagridview 在我的Win表单应用程序中,我还添加了一个 CheckBox 用于标记行。 这个 复选框 按我的预期工作,直到用户对 DataGridView . 排序后,先前选中的复选框列将丢失。

    有什么方法可以让我 控件 记住排序后选择了哪一行?

    2 回复  |  直到 8 年前
        1
  •  4
  •   Spell    9 年前

    有两个选项可以解决这个问题。

    第一个可能也是最简单的方法是将复选框列绑定到数据源。例如,如果使用DataTable作为数据源,则添加布尔列将在DataGridView上创建一个复选框,该复选框将进行排序,而不会丢失选中状态。

    如果这不是一个选项,那么解决问题的另一种方法是将DataGridView设置为 Virtual 模式并维护复选框值的缓存。

    看看优秀的 DataGridView FAQ 例如如何执行此操作。我还提供了以下代码,但请查看常见问题解答:

    private System.Collections.Generic.Dictionary<int, bool> checkState;
    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.DataSource = customerOrdersBindingSource;
    
        // The check box column will be virtual.
        dataGridView1.VirtualMode = true;
        dataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn());
    
        // Initialize the dictionary that contains the boolean check state.
        checkState = new Dictionary<int, bool>();
    }
    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        // Update the status bar when the cell value changes.
        if (e.ColumnIndex == 0 && e.RowIndex != -1)
        {
            // Get the orderID from the OrderID column.
            int orderID = (int)dataGridView1.Rows[e.RowIndex].Cells["OrderID"].Value;
            checkState[orderID] = (bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
        }    
    }
    
    private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
    {
        // Handle the notification that the value for a cell in the virtual column
        // is needed. Get the value from the dictionary if the key exists.
    
        if (e.ColumnIndex == 0)
        {
            int orderID = (int)dataGridView1.Rows[e.RowIndex].Cells["OrderID"].Value;
            if (checkState.ContainsKey(orderID))
                e.Value = checkState[orderID];
            else
                e.Value = false;
        }
    
    }
    
    private void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
    {
        // Handle the notification that the value for a cell in the virtual column
        // needs to be pushed back to the dictionary.
    
        if (e.ColumnIndex == 0)
        {
            // Get the orderID from the OrderID column.
            int orderID = (int)dataGridView1.Rows[e.RowIndex].Cells["OrderID"].Value;
    
            // Add or update the checked value to the dictionary depending on if the 
            // key (orderID) already exists.
            if (!checkState.ContainsKey(orderID))
            {
                checkState.Add(orderID, (bool)e.Value);
            }
            else
                checkState[orderID] = (bool)e.Value;
        }
    }
    
        2
  •  1
  •   Hans Olsson    15 年前

    我很惊讶会发生这种情况,但是如果在最坏的情况下没有其他方法可以绕过它,您可以将排序设置为编程的,然后在用户单击列标题时进行处理,保存检查项目的列表,以编程方式进行排序,然后检查任何应检查的项目。