2017-12-27 29 views
-1

Ich habe meinen Code wie folgt:Wählen Sie Werte aus kontrolliert Datagridview Artikel

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)); 
} 

, die die folgende Ausgabe erzeugt:

DataGrid

Und jetzt möchte ich einige Aktionen auszuführen, basierend auf welche Konten wurden ausgewählt (durch Umschalten der nachgestellten Kontrollkästchen), insbesondere M/C-Nummer.

Antwort

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 
    } 
} 
Verwandte Themen