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

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

问题描述

我在我的C#的形式添加了一个复选框列一个DataGridView。该功能必须是动态的 - 你选择客户,并带来了他们的全部物品,可以提供服务,而你选择要围绕服务这个时候这其中的

总之,code现在将一个chckbox添加到DGV的开头。我需要知道的是以下内容:

1)如何让这个整列选中默认?
2)我如何可以确保当我点击只是DGV?

下方的按钮,我只得到从选中行值

下面是要得到列插入code:

  DataGridViewCheckBoxColumn的doWork =新DataGridViewCheckBoxColumn();
            doWork.HeaderText =包括狗;
            doWork.FalseValue =0;
            doWork.TrueValue =1;
            dataGridView1.Columns.Insert(0,的doWork);

那么下一步是什么?
任何帮助将大大AP preciated!


解决方案

  1. 有没有办法直接做到这一点。一旦你有在网格中的数据,可以遍历行和检查每个盒子是这样的:

     的foreach(在dataGridView1.Rows的DataGridViewRow行)
    {
        row.Cells [CheckBoxColumn1.Name] .value的= TRUE;
    }


  2. Click事件可能是这个样子:

     私人无效的button1_Click(对象发件人,EventArgs的发送)
    {
        清单<&的DataGridViewRow GT; rows_with_checked_column =新的List<&的DataGridViewRow GT;();
        的foreach(在dataGridView1.Rows的DataGridViewRow行)
        {
            如果(Convert.ToBoolean(row.Cells [CheckBoxColumn1.Name] .value的)==真)
            {
                rows_with_checked_column.Add(行);
            }
        }
        //你想检查哪些行
    }


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.

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

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?

Here's the code to get the column inserted:

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. 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;
    }
    

  2. 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天全站免登陆