• Winform中实现List<string>赋值给dataGridView与实现多选、全选和获取选择的内容


    场景

    Winform中给DataGridView添加多选框列并获取选中行的内容:

    https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100066614

    在上面的基础上,实现了添加多选框并获取选中行的内容。

    如果要将List<string>作为dataGridView的数据源并实现多选和全选以及获取选择的内容怎么实现。

    注:

    博客:
    https://blog.csdn.net/badao_liumang_qizhi
    关注公众号
    霸道的程序猿
    获取编程相关电子书、教程推送与免费下载。

    实现

    1、将List<string>赋值给dataGridView的数据源

    this.dataGridView_show_tables_name.DataSource = this.tableNameList.Select(x => new { Value = x }).ToList();

    其中tableNameList就是List<string>

     

    2、实现多选框

                DataGridViewColumn checkCol = new DataGridViewCheckBoxColumn();
                checkCol.Name = "选择";
                this.dataGridView_show_tables_name.Columns.Add(checkCol);

    3、实现全选

    首先添加一个checkbox作为全选选择框

    然后实现其changed事件

            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                if (this.checkBox_select_all.Checked == true)
                {
                    for (int i = 0; i < this.dataGridView_show_tables_name.Rows.Count; i++)
                    {
                        this.dataGridView_show_tables_name.Rows[i].Cells["选择"].Value = 1;
                    }
                }
                else
                {
                    for (int i = 0; i < this.dataGridView_show_tables_name.Rows.Count; i++)
                    {
                        this.dataGridView_show_tables_name.Rows[i].Cells["选择"].Value = 0;
                    }
                }
            }

    在上面添加checkCol时设置了其Name属性。,所以这里通过Cells[|"选择"]获取到对应列。

    4、获取选择的内容

    首先声明变量来接收获取的内容

    List<string> selectedTableNameList = new List<string>();

    然后再需要获取选择内容的地方

                this.selectedTableNameList.Clear();
                for (int i = 0; i < this.dataGridView_show_tables_name.Rows.Count; i++)
                {
                    if ((bool)this.dataGridView_show_tables_name.Rows[i].Cells["选择"].EditedFormattedValue == true)
                    {
                        selectedTableNameList.Add(this.dataGridView_show_tables_name.Rows[i].Cells[1].Value.ToString());
                    }
                } 
    博客园: https://www.cnblogs.com/badaoliumangqizhi/ 关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。
  • 相关阅读:
    Error: unable to connect to node rabbit@mail: nodedown
    flash上传控件跨域
    Nginx配置指定媒体类型文件强制下载
    Nginx反向代理配置可跨域
    日志:using the Connector/J connection property 'autoReconnect=true' to avoid this problem
    web 安全(一)--web简介及 前端技术(js)
    Android 自动化之元素定位 xpath
    Wireshark入门
    自动化测试工具
    Android App 专项测试--压力测试篇(三)
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/15354153.html
Copyright © 2020-2023  润新知