简体   繁体   中英

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 checkboxes and assign them to a variables in array?

    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.

the problem is in the line

MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());

e.RowIndex points to the changed row

it should be:

    for (int i = 0; i <= this.dataGridView1.Rows)
    {
        var row = this.dataGridView1.Rows[i]
        if (Convert.ToBoolean(this.dataGridView1.Rows[i].Cells["chkBoxColumn"].Value = true) == true)
        {
            MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());
        }
    }   

Also, note that you're getting error in your second example because some row is containing a null value in the checkbox column.

Convert.ToBoolean(null) returns false

But bool.Parse(null) throws an exception

I guess something like this ?

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

    MessageBox.Show(text.ToString());
}

EDIT : the question has changed from showing the values to saving them into an array, so I need to change my answer also

Suppose you want all values for Cell[1] saved into an array
and suppose that cell is of type integer.

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{            
    List<int> list = new List<int>();

    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {
        if (Convert.ToBoolean(row.Cells["chkBoxColumn"].Value = true) == true)
        {
            list.Add(int.Parse(row.Cells[1].Value.ToString()));                
        }
    }   

    // you now have all values saved into the list
}

Edit
Question got changed, so tweaked the code so that the values get put into a list.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{            
    List<string> someList = new List<string>();

    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {
        var cell = row.Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;

        if (Convert.ToBoolean(cell.Value) == true)
        {
            if (cell.State != DataGridViewElementStates.Selected)
            {
                someList.Add(row.Cells[1].Value.ToString();
            }
        }
        else if (cell.State == DataGridViewElementStates.Selected)
        {
            someList.Add(row.Cells[1].Value.ToString();
        }
    }   
}

This might do the trick for you, however you'll get a popup every time a user ticks a checkbox, and you'll get as many popup as there are checkboxes that are true .

You can put the code in a button click and find checked values this way:

private void button1_Click(object sender, EventArgs e)
{
    var checkedValues = dataGridView1.Rows.Cast<DataGridViewRow>()
        .Where(row => (bool?)row.Cells["Your CheckBox Column Name"].Value == true)
        .Select(row => string.Format("{0}", row.Cells["The Other Column Name"].Value))
        .ToList();

    MessageBox.Show(string.Join(",", checkedValues));
}

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