這裏是加載代碼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++;
}
}
}
謝謝,但我如何使列表,因爲我使用複選框列來選擇行刪除 –
@ElijahAyeeta它已經在答案中了:'List deleteRows = new List ();'使行的列表被刪除,之後我遍歷該列表以實際刪除它們'foreach(DataGridViewRow在deleteRows中行)''。所以這個代碼片段應該可以插入並播放給你。 –
Huntt
喲這傢伙!!!!非常感謝!!!! –