C#通过拖动来更改datagridview单元格 [英] C# Change datagridview cells by dragging on them

查看:562
本文介绍了C#通过拖动来更改datagridview单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序从DataTable输出信息,我将表单sql数据收集到DataGridview中,DGV的最后一列是一个复选框。我想知道是否有任何快速的方式/事件我可以使用,所以每当用户点击并拖动进行大量选择(例如在图像中,单击第二个单元格并拖动),所有选定的单元格将其状态更改为如果用户点击了每个单独的复选框?

My program outputs information froma DataTable I collect form sql data into a DataGridview and the last column in the DGV is a checkbox. I want to know if there's any quick way/event I could use so whenever a user clicks and drags to make a large selection (like in the image, clicking on the second cell and dragging down), all the selected cells change their status as if the user had clicked on each individual checkbox?

目标是快速批量修改检查大型选择的方式,因为比检查每个复选框容易得多

The goal is to have a quick bulk-edit way to check large selections since that's a lot easier than checking each individual checkbox.

如果没有用于拖动选择的事件处理程序,我将如何获取大型选择(起始单元格,结束单元格)的边界?

If there's no event handler for drag-selecting, how would I get the bounds of a large selection (start cell, end cell)?

推荐答案

由于标准行为不适用于DGV CheckBoxesCells,而不用一点编码就是如何做到这一点,保持正常的Windows UI风格,即检查(和取消选择) CheckBoxes 空格栏:

As the standard behaviour deosn't work with DGV CheckBoxesCells without a little coding here is how I would do it, staying with the normal Windows UI style, that is checking (and unchecking) CheckBoxes with the Space bar:

private void DGV_KeyDown(object sender, KeyEventArgs e)
{
    if( e.KeyCode == Keys.Space && DGV.CurrentCell.ColumnIndex == yourCheckBoxColumn)
    {
        foreach (DataGridViewCell cell in DGV.SelectedCells)
            if (cell.ColumnIndex == yourCheckBoxColumn)     
                                    cell.Value = !((bool)cell.FormattedValue);
    }
}

我重复检查以避免交叉选择的崩溃。 。

I repeat the check to avoid crashes on cross selections..

请注意,我决定反转每个勾号;这样甚至复杂的选择都可以很容易地得到。我喜欢。您的里程可能有所不同:想要选出第一个单元格,并将所有单元格设置为与其值相反。这真的取决于您的用户!

Note that I decided to inverse each Checkmark; this way even complicated selection can be achived easily. I like that. Your mileage may vary: You may e.g. want to pick out the first Cell and set all cells to the opposite of its value.. This really depends on your users!

这篇关于C#通过拖动来更改datagridview单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆