[英]How do I in code check that my DataGridView checkbox is selected. c# visual studio Windows form
[英]How to check if multiple checkbox is selected or not in datagridview c#
我有一個DataGridview,可以選擇多個復選框。但是現在我需要收集選中的行數據並保留它。 如何檢查選中的行數以及如何保持選中的復選框行數據。
我已經做了很少的代碼。 但這僅保留一個選定的行數據。 這是我的代碼:
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();
}
好。 我從另一個評論部分獲得解決方案。這是我的解決方案-
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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.