2017-12-27 424 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 
    } 
}