简体   繁体   中英

How to check if multiple checkbox is selected or not in datagridview c#

I have a DataGridview where I can select multiple checkbox.but now I need to collected checked rows data and keep it. how can I check how many of my rows are selected and how can I keep me selected checkbox rows data.

I already did little code. but this only keep only one selected rows data. Here is my code:

DataGridViewCell datacelll = dgvSlsSRsList.CurrentCell;

if (datacelll != null)                   
{                           
    foreach (DataGridViewCheckBoxCell cell in dgvSlsSRsList.SelectedCells)
    {
        cell.Value = true;
        slssrsId = Convert.ToInt32(dgvSlsSRsList.Rows[e.RowIndex].Cells[1].Value);
        slssdpId = Convert.ToInt32(dgvSlsSRsList.Rows[e.RowIndex].Cells[2].Value);
        DueAmount = Convert.ToDecimal(dgvSlsSRsList.Rows[e.RowIndex].Cells[8].Value);
    }
    dgvSlsSRsList.Refresh();                            
}

ok. i get the solution from another post comment section.here is my solution-

                List<string> srlist = new List<string>();
                List<string> sdplist = new List<string>();
                List<string> dumamountlist = new List<string>();

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

                    if (Convert.ToBoolean(cell.Value) == true)
                    {
                        srlist.Add(row.Cells[1].Value.ToString());
                        sdplist.Add(row.Cells[2].Value.ToString());
                        dumamountlist.Add(row.Cells[8].Value.ToString());


                    }
                    else if (cell.State == DataGridViewElementStates.Selected)
                    {
                        srlist.Add(row.Cells[1].Value.ToString());
                        sdplist.Add(row.Cells[2].Value.ToString());
                        dumamountlist.Add(row.Cells[8].Value.ToString());
                    }

                }

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