mardi 2 janvier 2018

c# how to get the values next to checked datagridview checkboxes?

I have a DataGridView with CheckBox column, my question is how do I get the data next to a checked checkbox and print them all at once using MessageBox? I have tried these:

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {            
        foreach (DataGridViewRow row in this.dataGridView1.Rows)
        {
            if (Convert.ToBoolean(this.dataGridView1.Rows[e.RowIndex].Cells["chkBoxColumn"].Value = true) == true)
            {
                MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());
            }
        }   
    }

It prints the last checked item over and over.

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        for (int i = 0; i < dataGridView1.Rows.Count; i++)

        {
            if (bool.Parse(dataGridView1.Rows[i].Cells["chkBoxColumn"].Value.ToString()) == true)
            {
                MessageBox.Show(dataGridView1.Rows[i].Cells[1].Value.ToString());
            }
        }
    } 

and i'm getting a null reference exception using this.




Aucun commentaire:

Enregistrer un commentaire