2015-10-21 66 views
0

這裏是加載代碼DataGridView如何使用複選框列從DataGridView中刪除多個項目

private void btnLoaddata_Click(object sender, EventArgs e) 
{ 
    AddCheckBoxforDataGridView(); 
    try 
    { 
     conDB.Open(); 
     OleDbCommand command = new OleDbCommand(); 
     command.Connection = conDB; 
     command.CommandText = "select CWDetails,CCSpn_CODE as 'SPN CODE',CCFname as 'First Name',CCLname as 'Last Name',CCMname as 'Middle Name',CCDOB as 'Date Of Birth',CCgender as 'Gender',CCSchool as 'School',CaClass as 'Class',CCVillage as 'Village',CCSiblings as 'Number Of Siblings',CCGuardian as 'Guardian',CCContact as 'Contact',CCcurrentDt as 'Date Of Entry' from abaanaCC"; 
     // command.Parameters.Add(new OleDbParameter("@IMG", imageBt)); 
     // command.ExecuteNonQuery(); 
     //MessageBox.Show("Record Saved"); 

     OleDbDataAdapter da = new OleDbDataAdapter(command); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 

     EditdataGridView1.DataSource = dt; 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show("Unable to Load Data"); 
    } 
    conDB.Close(); 
} 

下面是增加了checkbox列到DataGridView

private void AddCheckBoxforDataGridView() 
{ 
    DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn() 
    { 
     Name = "Check" 
    }; 
    EditdataGridView1.Columns.Add(col); 
} 

代碼下面是創建刪除的方法。該CWDetails列在數據庫

public int DeleteMult(int CWDet) 
{ 
    conDB.Open(); 
    OleDbCommand command = new OleDbCommand(); 
    command.Connection = conDB; 
    string query = "delete from abaanaCC where CWDetails = " + CWDet + ""; 
    command.CommandText = query; 
    int res = command.ExecuteNonQuery(); 
    conDB.Close(); 
    return res; 
} 

primary key下面是刪除按鈕del_Mult這是不工作

private void del_Mult_Click(object sender, EventArgs e) 
{ 
    DataGridViewRow row = new DataGridViewRow(); 
    for (int i = 0; i < EditdataGridView1.Rows.Count; i++) 
    { 
     row = EditdataGridView1.Rows[1]; 
     if (Convert.ToBoolean(row.Cells[0].Value) == true) 
     { 
      int id = Convert.ToInt16(row.Cells[1].Value); 
      DeleteMult(id); 
      EditdataGridView1.Rows.Remove(row); 
      i++;      
     } 
    } 
} 

回答

0

您也可以嘗試做一個foreach循環,所以你不必惹int i,像這樣:

private void del_Mult_Click(object sender, EventArgs e) 
{ 
    List<DataGridViewRow> deleteRows = new List<DataGridViewRow>(); 

    foreach(DataGridViewRow row in EditdataGridView1.Rows) 
    { 
     if(Convert.ToBoolean(row.Cells[0].Value) == true) 
     { 
      int id = Convert.ToInt16(row.Cells[1].Value); 
      DeleteMult(id); 
      deleteRows.Add(row); 
     } 
    } 

    foreach(DataGridViewRow row in deleteRows) 
    { 
     EditdataGridView1.Rows.Remove(row); 
    } 
} 

雖然你必須要刪除的行的列表,因爲在忙於迭代它們時不能刪除行。

+0

謝謝,但我如何使列表,因爲我使用複選框列來選擇行刪除 –

+0

@ElijahAyeeta它已經在答案中了:'List deleteRows = new List ();'使行的列表被刪除,之後我遍歷該列表以實際刪除它們'foreach(DataGridViewRow在deleteRows中行)''。所以這個代碼片段應該可以插入並播放給你。 – Huntt

+0

喲這傢伙!!!!非常感謝!!!! –

0

檢索網格行的代碼,您沒有使用。你也不需要i ++因爲你已經在for循環中做了。

row = EditdataGridView1.Rows[i]; 

刪除我++的語句從如果條件。

+0

謝謝我現在可以刪除,除了一行,即如果我選擇3行,其中一個拒絕刪除 –

相關問題