1. 程式人生 > >c# datagridview裡面的checkbox全選和取消全選 按鈕

c# datagridview裡面的checkbox全選和取消全選 按鈕

首先

using System;

然後,設定全選button,選中所有的checkbox

        private void selectAll_Click(object sender, EventArgs e)
        {
        //遍歷datagridview中的每一行,判斷是否選中,若為選中,則選中
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if ((Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == false))
                {
                    dataGridView1.Rows[i].Cells[0].Value = "True";
                }
                else
                    continue;
            }
        }

接著,設定取消全選button,取消選中的所有checkbox

        private void cancelAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if ((Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true))
                {
                    dataGridView1.Rows[i].Cells[0].Value = "False";
                }
                else
                    continue;
            }
        }