如何更改整个datagridview列的单元格值? [英] How to change the cell value for entire datagridview column?

查看:51
本文介绍了如何更改整个datagridview列的单元格值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让datagridview列全部选中,或全部不选中.下面的代码有效,但是有没有更简单/更快的方法呢?要设置列中所有单元格的值?

I'd like for the datagridview column to check all, or check none. The code below works, but is there an easier/faster way of doing this? For setting the value for all the cells in a column?

if(searchResultsGrid.Columns["checkboxColumn"].HeaderText == "CheckAll")
{
    searchResultsGrid.Columns["checkboxColumn"].HeaderText = "UncheckAll";
    foreach (DataGridViewRow row in searchResultsGrid.Rows)
    {
        row.Cells["checkboxColumn"].Value = true;
        searchResultsGrid.UpdateCellValue(0, row.Index);
    }
}
else
{
    searchResultsGrid.Columns["checkboxColumn"].HeaderText = "CheckAll";
    foreach (DataGridViewRow row in searchResultsGrid.Rows)
    {
        row.Cells["checkboxColumn"].Value = false;
        searchResultsGrid.UpdateCellValue(0, row.Index);
    }
}

推荐答案

可以通过一些技巧来实现另一种方法.该列必须为 ReadOnly ,单元格值为 Null .单击列中的任何单元格将切换列的默认 NullValue ;本质上是在切换每个单元格.

You could achieve this another way through some trickery. The column must be ReadOnly and the cell values Null. Clicking any cell in the column will toggle the default NullValue of the column; essentially toggling every cell.

DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn(false);
chk.HeaderText = "CheckAll";
chk.Name = "checkboxColumn";
chk.ReadOnly = true;
this.dataGridView1.Columns.Add(chk);

this.dataGridView1.CellClick += DataGridView1_ToggleAll;


private void DataGridView1_ToggleAll(object sender, DataGridViewCellEventArgs e)
{
    DataGridViewCheckBoxColumn col = this.dataGridView1.Columns[e.ColumnIndex] as DataGridViewCheckBoxColumn;

    if (col?.Name == "checkboxColumn")
    {
        bool checkAll = (bool)col.DefaultCellStyle.NullValue;
        col.HeaderText = checkAll ? "CheckAll" : "UncheckAll";
        col.DefaultCellStyle.NullValue = !checkAll;
    }
}

但是请注意:要获取检查状态,请使用 cell.EditedFormattedValue not cell.Value .(因为要使此技巧起作用,必须的 Value 始终为 null .)

But note: to get the checked state you'd use cell.EditedFormattedValue and not cell.Value. (Because Value of necessity would always be null for this trick to work.)

这具有创造力,但您还必须考虑未来的开发人员.当您决定优化代码时,必须询问它如何影响维护.循环很容易理解,并且仍然可以快速执行.您必须权衡其价值.

This has a creative touch to it, but you must also think about future developers. When you decide to optimize code, you must ask how it will affect maintenance. Loops are easily understood and still execute quickly. You have to weigh the worth.

下面是我在100次运行中发现的摘录,每个摘录包含10,000行数据.这些是切换行的运行时间:

Below are snippets from my findings in 100 runs each with 10,000 rows of data. These were the run times of toggling the rows:

标准循环

Elapsed = 00:00:00.0315538
Elapsed = 00:00:00.0107964
Elapsed = 00:00:00.0676696
Elapsed = 00:00:00.0232370
Elapsed = 00:00:00.0243285

空值切换

Elapsed = 00:00:00.0006645
Elapsed = 00:00:00.0006712
Elapsed = 00:00:00.0006804
Elapsed = 00:00:00.0007579
Elapsed = 00:00:00.0003037

注意:尽管行数很多,但NullValue切换速度是一致的.对于小数据(1000行),循环比NullValue方法快2/3rd.

Note: NullValue toggling speed was consistent despite number of rows. For small data (1000 rows), looping was 2/3rd's faster than the NullValue method.

这篇关于如何更改整个datagridview列的单元格值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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