Hi everyone
I have a datagridview with a checkbox culumn. I want to check whether the check box cell is checked or unchecked. my code is as below.
it does work for the checked cell but when i uncheck one of the checked it does not remove it from the arraylist.

ArrayList subs=new ArrayList();
    
            
            foreach(DataGridViewRow row in dgv_sub.Rows)
            {
                if ((bool)row.Cells[0].Value !=null)//Checked
                {
                    string su = row.Cells[1].Value.ToString();
                    subs.Add(su);
                }
           }
           foreach (string s in subs)
            {
                MessageBox.Show(s);
            }
            subs.Clear();

I actually have a sample project here that does that.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.dgv
{
  public partial class frmDataGridView5 : Form
  {
    private DataTable dt;
    private DGVColumnHeader colHead;
    private bool suspendEvents;

    public frmDataGridView5()
    {
      InitializeComponent();
    }

    /// <summary>
    /// Gets simulatation data
    /// </summary>
    /// <returns></returns>
    private static DataTable GetTestData()
    {
      DataTable result = new DataTable();
      result.Columns.Add(new DataColumn("RecordId", typeof(int)));
      result.Columns.Add(new DataColumn("Name", typeof(string)));
      for (int i1 = 1000; i1 <= 1008; i1++)
      {
        DataRow row = result.NewRow();
        row["RecordId"] = i1;
        row["Name"] = Guid.NewGuid().ToString();
        result.Rows.Add(row);
      }
      return result;
    }

    private void frmGridView5_Load(object sender, EventArgs e)
    {
      //DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
      //col1.Name = "colName";
      //col1.DataPropertyName = "RecordId";

      dt = GetTestData();
      dataGridView1.DataSource = dt;

      //initialize DGVColumnHeader object
      colHead = new DGVColumnHeader();

      //Add columns dynamically  to gridview
      dataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn());
      dataGridView1.Columns[0].HeaderCell = colHead;

    }

    private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
      if (suspendEvents)
        return;

      
      dataGridView1.EndEdit();
      if (e.ColumnIndex == 0)
      {
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
          if (!dataGridView1.Rows[i].IsNewRow)
            dataGridView1.Rows[i].Cells[0].Value = colHead.Checked;
        }
      }
    }

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      if (suspendEvents)
        return;

      if (e.ColumnIndex == 0)
      {
        bool allChecked = true;
        
        for (int i = 0; i < this.dataGridView1.RowCount; i++)
        {
          if (this.dataGridView1.Rows[i].IsNewRow)
            continue;

          bool colChecked = string.Compare(this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString(), "true", true) == 0;
          if (i == e.RowIndex)
            colChecked = !colChecked;

          if (!colChecked)
          {
            allChecked = false;
            break;
          }
        }

        suspendEvents = true;
        try
        {
          colHead.Checked = allChecked;
          dataGridView1.InvalidateColumn(0);
        }
        finally
        {
          suspendEvents = false;
        }
      }
    }
  }

  public class DGVColumnHeader : DataGridViewColumnHeaderCell
  {
    private Rectangle CheckBoxRegion;
    private bool _checked = false;

    protected override void Paint(Graphics graphics,
        Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
        DataGridViewElementStates dataGridViewElementState,
        object value, object formattedValue, string errorText,
        DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
        DataGridViewPaintParts paintParts)
    {

      base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value,
          formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

      graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);

      CheckBoxRegion = new Rectangle(
          cellBounds.Location.X + 1,
          cellBounds.Location.Y + 2,
          25, cellBounds.Size.Height - 4);


      if (this._checked)
        ControlPaint.DrawCheckBox(graphics, CheckBoxRegion, ButtonState.Checked);
      else
        ControlPaint.DrawCheckBox(graphics, CheckBoxRegion, ButtonState.Normal);

      Rectangle normalRegion =
          new Rectangle(
          cellBounds.Location.X + 1 + 25,
          cellBounds.Location.Y,
          cellBounds.Size.Width - 26,
          cellBounds.Size.Height);

      graphics.DrawString(value.ToString(), cellStyle.Font, new SolidBrush(cellStyle.ForeColor), normalRegion);
    }

    protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
    {
      //Convert the CheckBoxRegion 
      Rectangle rec = new Rectangle(new Point(0, 0), this.CheckBoxRegion.Size);
      this._checked = !this._checked;
      if (rec.Contains(e.Location))
      {
        this.DataGridView.Invalidate();
      }
      base.OnMouseClick(e);
    }

    public bool Checked
    {
      get { return this._checked; }
      set { this._checked = value; }
    }
  }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.