2017-12-27 493 views
-1

我有我的代碼如下:從檢查的DataGridView項目選擇值

DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn(); 
actGrid.Columns.Add(chk); 
chk.HeaderText = "Select"; 
chk.Name = "select"; 
chk.ReadOnly = false; 

DataGridViewTextBoxColumn mc_no = new DataGridViewTextBoxColumn(); 
actGrid.Columns.Add(mc_no); 
mc_no.HeaderText = "M/C Number"; 
mc_no.Name = "mc_no"; 
mc_no.Width = 200; 
mc_no.ReadOnly = true; 

DataGridViewTextBoxColumn act_name = new DataGridViewTextBoxColumn(); 
actGrid.Columns.Add(act_name); 
act_name.HeaderText = "Name"; 
act_name.Name = "member"; 
act_name.Width = 262; 
act_name.ReadOnly = true; 

while (DR.Read()) 
{ 
    actGrid.Rows.Add(true, DR.GetInt32(0).ToString(), DR.GetString(2) + " " + DR.GetString(1)); 
} 

將會產生以下的輸出:

DataGrid

,現在我想基於哪些帳戶執行某些操作(通過切換尾部複選框),尤其是M/C號碼。

回答

1
// iterate over DataGridView rows 
foreach (DataGridViewRow row in actGrid.Rows) 
{ 
    // check, if row is selected by checkbox 
    if (Equals(row.Cells["select"].Value, true)) 
    { 
     // get values for selected row 
     var mc_no_Value = (string)row.Cells["mc_no"].Value; 
     var member_Value = (string)row.Cells["member"].Value; 

     // do smth with values here 
    } 
}