对选中的datagridview中的多行,不能正确删除,只能删除选定的最后一个,究竟问题出在哪?代码:
private void btnDel_Click(object sender, EventArgs e)
{
int i = this.dataGridView1.SelectedRows.Count; DialogResult dr = MessageBox.Show("删除后不能恢复,确实想删除这"+Convert.ToString(i) +"个用户信息吗?", "友情提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
{
int t = 0;
try
{
for (int j = 0; dataGridView1.SelectedRows.Count > j; )
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
} DBCon oleCon = new DBCon();
string strSql = "delete from UserInfo where 姓名='" + username + "'";
t = oleCon.Updata(strSql); if (t == 0)
{
MessageBox.Show("删除失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
string Sql = "select * from UserInfo";
ds = oleCon.Query(Sql);
this.dataGridView1.DataSource = ds.Tables[0];
MessageBox.Show("删除成功!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
return;
}
} public int Updata(string strSql)
{
int t = 0;
using (OleDbConnection oleCon = new OleDbConnection(strCon))
{
oleCon.Open();
OleDbCommand oleCom = new OleDbCommand();
oleCom.Connection = oleCon;
oleCom.CommandText = strSql;
t = oleCom.ExecuteNonQuery();
}
return t;
}
private void btnDel_Click(object sender, EventArgs e)
{
int i = this.dataGridView1.SelectedRows.Count; DialogResult dr = MessageBox.Show("删除后不能恢复,确实想删除这"+Convert.ToString(i) +"个用户信息吗?", "友情提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
{
int t = 0;
try
{
for (int j = 0; dataGridView1.SelectedRows.Count > j; )
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
} DBCon oleCon = new DBCon();
string strSql = "delete from UserInfo where 姓名='" + username + "'";
t = oleCon.Updata(strSql); if (t == 0)
{
MessageBox.Show("删除失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
string Sql = "select * from UserInfo";
ds = oleCon.Query(Sql);
this.dataGridView1.DataSource = ds.Tables[0];
MessageBox.Show("删除成功!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
return;
}
} public int Updata(string strSql)
{
int t = 0;
using (OleDbConnection oleCon = new OleDbConnection(strCon))
{
oleCon.Open();
OleDbCommand oleCom = new OleDbCommand();
oleCom.Connection = oleCon;
oleCom.CommandText = strSql;
t = oleCom.ExecuteNonQuery();
}
return t;
}
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
}
不用++或者--吗?
楼主定义的i为什么不用
int i = this.dataGridView1.SelectedRows.Count; DialogResult dr = MessageBox.Show("删除后不能恢复,确实想删除这"+Convert.ToString(i) +"个用户信息吗?", "友情提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
}
楼主在这里,已经把所有选中的行一次全删除掉了。
DBCon oleCon = new DBCon();
string strSql = "delete from UserInfo where 姓名='" + username + "'";
t = oleCon.Updata(strSql);
再到数据库删除的话,能删多条才奇怪.
for (int j = 0; dataGridView1.SelectedRows.Count > j; )
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
}
这里面来.
private void btnDelete_Click(object sender, EventArgs e)
{
//判断用户是否选择一行数据,true为没选择,false为选择
if (this.dgv.Rows[this.dgv.CurrentRow.Index].Cells[0].Value.ToString()=="")
{
MessageBox.Show("请选择一项进行删除");
}
else
{
//判断用户是否点击确定按钮,true为点击,false为没有点击
if (MessageBox.Show("确认删除?","提示", MessageBoxButtons.YesNo)==DialogResult.Yes)
{
//定义数组,用循环赋值 String[] array = new String[];
for (int i = 0; i < this.dgv.SelectedRows.Count; i++)
{
String str = this.dgv.Rows[this.dgv.SelectedRows[i].Index].Cells[0].Value.ToString();
String strDelete = "Delete from students where StudentNumber='" + str + "'";
array[i] = strDelete;
}
//遍历数组
foreach (String str in array)
{
this.Update(str);
}
//这里写刷新的方法
}
}
}
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
}
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[j]);
}
usename = dataGridView1.SelectedRows[j].Cells["username"];string strSql = "delete from UserInfo where 姓名='" + username + "'";
我是先将数据库中的数据全部删除,
然后把表格里面的数据重新添加到数据库中,这样就不用一行一行的改呀删呀的,太麻烦. //保存
private void button1_Click(object sender, EventArgs e)
{
try
{
//先删除
string strSql2;
strSql2 = "delete from JC颜色";
clsSql.Ex(strSql2);
//后保存
for (int i = 0; i < this.ucDataGridView1.RowCount - 1; i++)
{
string _ID, _ZNname, _CNname, _Coloer; if (ucDataGridView1.Rows[i].Cells[0].Value == null)
{
_ID = "";
}
else
{
_ID = ucDataGridView1.Rows[i].Cells[0].Value.ToString();
} if (ucDataGridView1.Rows[i].Cells[1].Value == null)
{
_ZNname = "";
}
else
{
_ZNname = ucDataGridView1.Rows[i].Cells[1].Value.ToString();
}
if (ucDataGridView1.Rows[i].Cells[2].Value == null)
{
_CNname = "";
}
else
{
_CNname = ucDataGridView1.Rows[i].Cells[2].Value.ToString();
}
if (ucDataGridView1.Rows[i].Cells[3].Value == null)
{
_Coloer = "";
}
else
{
_Coloer = ucDataGridView1.Rows[i].Cells[3].Value.ToString();
} if (_ID == "" )
{
continue;
}
string strSql;
strSql = "insert into JC颜色(编号,名称,英文名称,PANTONE色卡)" +
"values(" +
"'" + _ID + "','" + _ZNname + "','" + _CNname + "','" + _Coloer + "'" +
")";
if (Exist(_ID))
{
MessageBox.Show("编号" + _ID + "已存在!");
return;
}
clsSql.Ex(strSql);
}
MessageBox.Show("保存数据成功!");
}
catch (Exception )
{
MessageBox.Show("编号一定要输入!");
}
}
{
if (dgvRow.IsNewRow)
MessageBox.Show("无法删除未提交的新行!", "操作提示");
else
dgvCardManage.Rows.Remove(dgvRow); //删除选中行
}