• DataGridView 添加checkbox


    DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
    newColumn.HeaderText = "选择";
    dataGridView1.Columns.Add(newColumn);

    这样添加的列是放在最后一列,也许你希望它在其它列,例如第二列,那么可以:
    dataGridView1.Columns.Insert(1, newColumn);

    DataGridView中的DataGridViewCheckBoxColumn是很难控制的,当 DataGridView 未绑定

    到数据时,对单元格的编辑会在用户移到另一个单元格时被交        

    DataGridView.IsCurrentCellDirty 属性

    如果当前单元格有未提交的更改,为 true;否则为 false。处理方法

    if (dataGridView.IsCurrentCellDirty)       

    {//将当前单元格中的更改提交到数据缓存,但不结束编辑模式。                 dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);

    }

    在winfrom 的DataGridView中添加一列,ColumnType属性设置为DataGridViewCheckBoxColumn。

    假设设置Name属性为SelIndex

    1、选择全部列

            private void btnSelALl_Click(object sender, EventArgs e)
            {
                DataTable dt = (dataGridView1.DataSource as DataTable);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["SelIndex"] = 1;
                }
            }

    2、取得某一选择行的值

    private void btnGetRows_Click(object sender, EventArgs e)
            {

                //刷新
                if (dataGridView1.IsCurrentCellDirty)
                {
                    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }

               //取得选中的行
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (dataGridView1.Rows[i].Cells["SelIndex"].Value.ToString() == "1")
                    {
                        //相应的操作

                      MessageBox.Show( dataGridView1.Rows[i].Cells["Test"].Value.ToString());

                    }
                }

            }

    其他:添加 一行(比如说合计行)

    index = dataGridView1.Rows.Add();
    DataGridViewRow newrow = dataGridView1.Rows[index];
    newrow.Cells[0].Value = ...;
    newrow.Cells[1].Value = ...;
    newrow.Cells[2].Value = ...

    3

    首先datagridview属性--> 编辑列--> DataGridViewCheckBoxColumn中的数据有设falsevalue和truevalue
    C# code

    if (this.checkBox1.Checked) { for (int i = 0; i < this.dataGridView1.Rows.Count; i++) { this.dataGridView1.Rows[i].Cells["Column0"].Value = 1; } } else { for (int i = 0; i < this.dataGridView1.Rows.Count; i++) { this.dataGridView1.Rows[i].Cells["Column0"].Value = 0; }

    }

        

    // checkbox 勾上

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if ((bool)dataGridView1.Rows[e.RowIndex].Cells[0].EditedFormattedValue == true)
                {
                    dataGridView1.Rows[e.RowIndex].Cells[0].Value = false;
                }
                else
                {
                    dataGridView1.Rows[e.RowIndex].Cells[0].Value = true;
                }
            }

    我的全选是这样的,是可以的呀
       //循环dataGridView
       for (int i = 0; i < dgvOperation.Rows.Count; i++)
       {
           //设置设置每一行的选择框为选中,第一列为checkbox
           dgvOperation.Rows[i].Cells[0].Value = true;
       }
    这个是反选的作用
    //循环dataGridView
    for (int i = 0; i < dgvOperation.Rows.Count; i++)
    {
    //判断当前行是否被选中
      if ((bool)dgvOperation.Rows[i].Cells[0].EditedFormattedValue == true)
          //设置每一行的选择框为未选中
         dgvOperation.Rows[i].Cells[0].Value = false;
      else
         //设置每一行的选择框为选中
         dgvOperation.Rows[i].Cells[0].Value = true;
     }
  • 相关阅读:
    一些比较水的题目
    oracle not in,not exists,minus 数据量大的时候的性能问题
    简单的oracle分页语句
    oracle 查询结果集运算
    Spring注解详解
    HTTP报头Accept 和 Content-Type的区别
    vue 实现分转元的 过滤器
    oracle or语句的坑
    CSS样式 让你的输入的小写自动变成大写。
    js 十分精确身份证验证
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/2007319.html
Copyright © 2020-2023  润新知