L. Levine
L. Levine

Reputation: 155

Validating a Checkbox in a datagrid C#

I have an unbound datagrid that has two columns that are defined as checkboxes.

What I'm trying to accomplish is performing validation when a user clicks on any column that appears in the delete column. In some instances I don't want the user to be able to delete certain records.

While working on this, I discovered that neither the CellValidating or CellValueChanged methods are getting called each time I click in a cell in the Delete column.

I've read similar questions but I have yet to find exactly what I'm trying to accomplish.

Thank you in advance for your time and effort in answering my question.

enter image description here

var checkBox = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;

            var isCheck = checkBox?.Value;
            var check = isCheck == null ? false : bool.Parse(isCheck.ToString());

            if (isCheck != null)
                checkBox.Value = !check;        // change checkbox value

            if (!check) // if value was false, it's being changed to true
            {
                string sShipToId = dataGridView1.Rows[e.RowIndex].Cells[(int)ColumnHeaders.ShipToIDColumn].Value.ToString();
                string sDelete = dataGridView1.Rows[e.RowIndex].Cells[(int)ColumnHeaders.DeleteColumn].Value.ToString();


                // need to check to see if this ship to is associated with an open order. if it is
                // we can't delete it. This mimics the functionality that P21 exhibits when you try to delete
                // a ship to from the Ship To Maintenance screen.
                // we also need to check and see if we're deleting the Ship to associated with the  current order.

                ShipToInfo Ship = new ShipToInfo(Server, Database);

                if ((string.IsNullOrEmpty(sShipTo) == false) &&
                        (sShipToId.Equals(sShipTo) == true) ||
                        (Ship.ShipToExistsInOpenOrder(sShipToId, CompanyID) == true))
                {
                    MessageBox.Show("Open orders exist for this Ship To " + sShipToId + ". It cannot be deleted.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    dataGridView1_CellContentClick(null, new DataGridViewCellEventArgs(e.ColumnIndex, e.RowIndex));
                }
            }

Upvotes: 1

Views: 1323

Answers (2)

L_J
L_J

Reputation: 2439

You can use the CellContentClick event to work with Checkboxes in DataGridView. I dynamically created the DataGridView object MyDataGridView. You can do it using designer as well. I am not sure how you want to use the value of the CheckBox but if you use it you should change its value manually in CellContentClick event as it is below.

private DataGridView MyDataGridView = new DataGridView();

public Form1()
{
    InitializeComponent();
    SetupDataGridView();
}

private void SetupDataGridView()
{
    this.Controls.Add(MyDataGridView);
    MyDataGridView.ColumnCount = 2;

    MyDataGridView.Name = "MyDataGridView";
    MyDataGridView.Location = new Point(10, 10);
    MyDataGridView.Size = new Size(500, 300);

    MyDataGridView.Columns[0].Name = "ID";
    MyDataGridView.Columns[1].Name = "Value";

    MyDataGridView.Columns.Add(new DataGridViewCheckBoxColumn { Name = "Default" });
    MyDataGridView.Columns.Add(new DataGridViewCheckBoxColumn { Name = "Delete" });

    MyDataGridView.Rows.Add("1", "one", true, true);
    MyDataGridView.Rows.Add("2", "two", false, true);
    MyDataGridView.Rows.Add("3", "three", true, false);

    MyDataGridView.CellContentClick += MyDataGridView_CellContentClick;
}

private void MyDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    // get value of checkbox
    var checkBox = MyDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
    var isCheck = checkBox?.Value;
    var check = isCheck == null ? false : bool.Parse(isCheck.ToString());

    if (isCheck != null)            
        checkBox.Value = !check;        // change checkbox value

    if (e.ColumnIndex == 3 && check)
    {
        DialogResult dialogResult = MessageBox.Show("Are you Sure", 
            "Delete Row", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            MyDataGridView.Rows.RemoveAt(e.RowIndex);
        }
    }
}

Upvotes: 1

Gaurav Jalan
Gaurav Jalan

Reputation: 497

Add an event for Checked in the checkbox, and apply your logic there. https://www.dotnetperls.com/checkbox-wpf

Upvotes: 1

Related Questions