DataGridView 复选框列 - 值和功能 [英] DataGridView checkbox column - value and functionality

查看:63
本文介绍了DataGridView 复选框列 - 值和功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C# 表单中向 DataGridView 添加了一个复选框列.该功能需要是动态的 - 您选择一个客户并调出他们所有可以服务的项目,然后您选择这次希望为他们中的哪些项目提供服务.

I've added a checkbox column to a DataGridView in my C# form. The function needs to be dynamic - you select a customer and that brings up all of their items that could be serviced, and you select which of them you wish to be serviced this time around.

无论如何,代码现在会在 DGV 的开头添加一个 chckbox.我需要知道的是以下内容:

Anyway, the code will now add a chckbox to the beginning of the DGV. What I need to know is the following:

1) 如何使整个列在默认情况下被选中"?2) 当我点击 DGV 正下方的按钮时,如何确保我只从选中"行中获取值?

1) How do I make it so that the whole column is "checked" by default? 2) How can I make sure I'm only getting values from the "checked" rows when I click on a button just below the DGV?

这是插入列的代码:

DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
            doWork.HeaderText = "Include Dog";
            doWork.FalseValue = "0";
            doWork.TrueValue = "1";
            dataGridView1.Columns.Insert(0, doWork);

接下来呢?任何帮助将不胜感激!

So what next? Any help would be greatly appreciated!

推荐答案

  1. 没有办法直接做到这一点.将数据放入网格后,您可以遍历行并检查每个框,如下所示:

  1. There is no way to do that directly. Once you have your data in the grid, you can loop through the rows and check each box like this:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    row.Cells[CheckBoxColumn1.Name].Value = true;
}

  • Click 事件可能如下所示:

  • The Click event might look something like this:

    private void button1_Click(object sender, EventArgs e)
    {
        List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (Convert.ToBoolean(row.Cells[CheckBoxColumn1.Name].Value) == true)
            {
                rows_with_checked_column.Add(row);
            }
        }
        // Do what you want with the check rows
    }
    

  • 这篇关于DataGridView 复选框列 - 值和功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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