DataGridView控件中添加删除按钮,进行选中行的删除操作。
我在DataGridView控件绑定数据源后使用代码添加按钮(删除按钮),但是当执行一次删除之后,就会多出一列删除列,这是什么原因啊????
代码如下:
//添加按钮列
dataGridView1.DataSource = ds.Tables[0].DefaultView;
DataGridViewButtonColumn ColumnDelete = new DataGridViewButtonColumn();
ColumnDelete.HeaderText = "删除";
ColumnDelete.Name = "删除";
ColumnDelete.Text = "删除";
this.dataGridView1.Columns.Add(ColumnDelete);
//删除操作
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "delete")
{
//sql删除语句
}
}
当执行第一次删除时是成功的,但是成功之后多出一列删除列????这是什么情况???
[解决办法]
conn = new SqlConnection(SQL.conStr); conn.Open(); int row = this.dataGridView2.CurrentCell.RowIndex; if (dataGridView2.CurrentRow != null) { if (MessageBox.Show("确定要删除该行数据吗?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { try { string ind = this.dataGridView2.Rows[row].Cells["流程环节编号"].Value.ToString(); string inds = this.dataGridView2.Rows[row].Cells["主机型号"].Value.ToString(); SqlCommand cmDelete = new SqlCommand("delete from hostmodel_process where process_id = '" + ind + "' and host_model='"+inds+"'", conn); cmDelete.ExecuteNonQuery(); MessageBox.Show("删除成功!"); string sql = "select host_model as 主机型号,process_id as 流程环节编号 from hostmodel_process order by host_model,process_id asc"; SDA1 = new SqlDataAdapter(sql, conn); DT1 = new System.Data.DataTable(); SDA1.Fill(DT1); this.dataGridView2.DataSource = DT1; cmDelete.Dispose(); conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }