我在datagridview里面新增了datagridviewcheckboxcolumn,我想选择两行记录,按删除按钮,同时删除了这两行,改怎么做呢?
private void button3_Click(object sender, EventArgs e)
{
DialogResult bt = MessageBox.Show("你确定要删除此条记录吗?", "请确认", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (bt == DialogResult.Yes)//点击yes时发生
{
int count = Convert.ToInt32(dataGridView1.Rows.Count.ToString());
try
{
for (int i = 0; i < count; i++)
{
//如果DataGridView是可编辑的,将数据提交,否则处于编辑状态的行无法取到
//dataGridView1.EndEdit();
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true) //查找被选择的数据行
{
model();
bll.delete(friends);
//弹出对话框
MessageBox.Show("删除成功!");
DataSet ds = bll.AllfriendsInfo();
bd.DataSource = ds.Tables[0];
this.dataGridView1.DataSource = bd;
//调用下面的方法
buttonchange1();
}
else
{
MessageBox.Show("删除失败!"); }
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
} } }
我这样做调试时,总是先删除一条,然后就说索引错误之类的,请高手帮忙知错啊!
private void button3_Click(object sender, EventArgs e)
{
DialogResult bt = MessageBox.Show("你确定要删除此条记录吗?", "请确认", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (bt == DialogResult.Yes)//点击yes时发生
{
int count = Convert.ToInt32(dataGridView1.Rows.Count.ToString());
try
{
for (int i = 0; i < count; i++)
{
//如果DataGridView是可编辑的,将数据提交,否则处于编辑状态的行无法取到
//dataGridView1.EndEdit();
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true) //查找被选择的数据行
{
model();
bll.delete(friends);
//弹出对话框
MessageBox.Show("删除成功!");
DataSet ds = bll.AllfriendsInfo();
bd.DataSource = ds.Tables[0];
this.dataGridView1.DataSource = bd;
//调用下面的方法
buttonchange1();
}
else
{
MessageBox.Show("删除失败!"); }
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
} } }
我这样做调试时,总是先删除一条,然后就说索引错误之类的,请高手帮忙知错啊!
如果不是继续
DataSet ds = bll.AllfriendsInfo();
bd.DataSource = ds.Tables[0];
this.dataGridView1.DataSource = bd;
的放错地方了,我应该把所有行删除后再更新?
i=0时删除,第一条成功,i++ -> i=1,count=2 dataGridView1中下一条数据顶上,变成第一条数据
这时候不存在第二条数据,你却去操作这第二条数据
for (int i = 1; i <= count; i++)
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true) //查找被选择的数据行
{
model();
bll.delete(friends); }
}
//弹出对话框
MessageBox.Show("删除成功!");
DataSet ds = bll.AllfriendsInfo();
bd.DataSource = ds.Tables[0];
this.dataGridView1.DataSource = bd;
{
model();
bll.delete(friends);
i--;
count--;
}
红字部分加下,试试看看
不行,还是只能删除其中一行,,,
会不会跟我的DAL层有关呢?
#region 删除好友
public bool deletefriends(MODEL .model .friends delete)//删除好友
{
cn.Open();
SqlCommand cm = new SqlCommand("delete from dbo.friends where fname='" + delete.Fname + "';", cn);
int d = (int)cm.ExecuteNonQuery();
cn.Close();
if (d > 0)
{
cn.Close();
return true;
}
else
{
cn.Close();
return false;
} }
#endregion
for (int i = 1; i <= count; i++)
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i-1].Cells[0];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true) //查找被选择的数据行
{
model();
bll.delete(friends);
i--;
count--; }
}
//弹出对话框
MessageBox.Show("删除成功!");
DataSet ds = bll.AllfriendsInfo();
bd.DataSource = ds.Tables[0];
this.dataGridView1.DataSource = bd;
dataGridView1的第一条数据下标是0
你 看下进入你DAL层的次数
我感觉是我的SQL语句写的不对吧,只能删掉我选中的名字,另一个名字没有选中就没有删掉,可是我要怎么写呢
{
..............
}
this.dataGridView1.DataSource = bd;
确实是在DAL出错,第二次进入的时候,跳到
else
{ return false;
}
就没有删了,到底该怎么写好
lz可以改变思路,在你的for循环中只是记录要删除的行,但不进行删除的操作。也不需要重新的绑定;
循环得到所有的要被删除的行后,直接操作你的库,执行删除;最后在从数据库中重新查一遍你要的数据再次绑定
SqlCommand cm = new SqlCommand("delete from dbo.friends where fname='" + delete.Fname + "';", cn);
我这样写只能删除一个
断点调试时,看看每次你传到dal层delete.Fname的值,是不是和你当前处理的dataGridView1里的数据值
我断点了啊,也看了调试,知道只删除其中一个显示在我的文本框的名字,另一个没有显示在文本框的名字在执行时就跳到return false了,我的方法model()里面定义了字段fname=文本框.text,,是不是因为这个,还是我的SQL语句
第一个名字删除后,fnametextbox里面显示的还是已经删除的那个名字,所以找不到那行,所以跳到return false了,我想了好久,想改SQL语句,但不知道怎么改,真的不知道怎么改好 #region 删除好友
private void button3_Click(object sender, EventArgs e)
{
DialogResult bt = MessageBox.Show("你确定要删除此条记录吗?", "请确认", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (bt == DialogResult.Yes)//点击yes时发生
{
int count = Convert.ToInt32(dataGridView1.Rows.Count.ToString());
for (int i =0 ; i <= count-1;i++ )
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
bool flag = Convert.ToBoolean (checkCell.Value);
if (flag == true) //查找被选择的数据行
{
model();
i++;
}
}
bll.delete(friends);
//弹出对话框
MessageBox.Show("删除成功!");
//调用下面的方法
bing();
buttonchange1();
}
else
{
MessageBox.Show("删除失败!");
} }
#endregion
{
model();
i++;
设置fnametextbox的值为当前处理记录的name
}
if (flag == true) //查找被选择的数据行
{
model();
friends .Fname = dataGridView1.Rows[i].Cells["fname"].Value.ToString().Trim();
bll.delete(friends);
}我就加了中间那句,就可以删了。