简体   繁体   中英

Show message box when the check box in the datagridview is checked

I have a database like this (view on DataGridView ):

在此处输入图片说明

As you can see on the image above, there are checkboxes in the DataGridView . I want when the checkbox is checked, the messagebox show a some text.

How can I achieve that?

What I have tried is whenever I checked the checkbox, the messagebox never appear and the first row of January 2014 column is checked when I run the program, but when I uncheck and check again, the messagebox not appearing.

Here is the code that I am using:

private void Amount(object sender, DataGridViewCellEventArgs e)
        {
            DataTable _dt = (DataTable)dataGridView1.DataSource;

            foreach(DataGridViewRow _row in _dt.Rows)
            {
                if (Convert.ToBoolean(_row.Cells["4"].Value) == true)
                {
                    MessageBox.Show("Checked");
                }
            }

        }

And I called it in:

dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(this.Amount);

I added the checkboxes by manually through database, here is my access database looks like:

在此处输入图片说明

Your answer much appreciated! Thank you!

UPDATE:

The code below is the code from Sir Daniloloko that I have modified, I used the List<int> , because the ColumnIndex that I want to check is more than 1:

dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(this.Amount);

List<int> _columnIndexes;

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.IsCurrentCellDirty)
            {
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

private void Amount(object sender, DataGridViewCellEventArgs e)
        {
            _columnIndexes = new List<int>(new int[] { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 });

            foreach (int index in _columnIndexes)
            {
                if (e.ColumnIndex == index)
                {
                    var value = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

                    if (value != null)
                    {
                        if (Convert.ToBoolean(value) == true)
                        {
                            MessageBox.Show("Checked. \n" + "Column Number: " + e.ColumnIndex + "", "Checked");
                        }

                        else
                        {
                            MessageBox.Show("Not Checked. \n" + "Column Number: " + e.ColumnIndex + "", "Not Checked");
                        }
                    }
                }
            }
        }

But from the code above, still the MessageBox.Show("Not Checked"); or MessageBox.Show("Checked"); is not showing whenever I checked or unchecked the CheckBox(es) in the DataGridView (On the runtime). What I want is the MessageBox is show whenever right after I checked or unchecked the CheckBox(es), not when I hit Enter or Update button then the MessageBox is shows (depending my checkbox(es) is unchecked or checked)

Thank you very much!

Sorry for confusing you guys more.

Solved:

I forgot to declared the dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(this.DirtyStateChanged); in the Form_Load .

Thank you very much and sorry for confusing you guys.

Regards.

If you associate Amount to an event named CellValueChanged it will rise every time you load the GRID. Resolved!

However if you need to detect the row thats you checked after the load of the grid, you will need to use the event named CurrentCellDirtyStateChanged of dataGridView1:

EDIT: dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(this.DirtyStateChanged); // Declared and put this on the Form_Load dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(this.DirtyStateChanged); // Declared and put this on the Form_Load

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

And your CellValueChanged event of dataGridView1 will be like this:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        var value = ((DataGridView) sender).Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
    }
}

UPDATE: Your method will be this:

private void Amount(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == N)//YOUR CHECKBOX COLUM INDEX
        {
            var value = ((DataGridView) sender).Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            if (value != null)
            {
                if (Convert.ToBoolean(value) == true)
                    MessageBox.Show("Checked");
                else
                    MessageBox.Show("Not Checked");
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM