求教高手,下面的代码,执行结果总是只能删除两行,头疼啊,学习中,代码较乱,见笑哈
private void Form1_Load(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd = con.CreateCommand();
cmd.CommandText = "select * from Employees";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
column.HeaderText = "Select";
column.Name = "select";
column.ReadOnly = false;
dataGridView1.Columns.Add(column);
dataGridView1.AllowUserToAddRows = false; }
} private void bttnDelete_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd = con.CreateCommand();
cmd.CommandText = "delete from Employees where EmployeesID=@id";
DataTable table = (DataTable)dataGridView1.DataSource;
int i=0;
MessageBox.Show(dataGridView1.RowCount.ToString()); do
{
string setl = dataGridView1.Rows[i].Cells["select"].EditedFormattedValue.ToString();
if (setl == "True")
{
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("id", dataGridView1.Rows[i].Cells[0].Value));
table.Rows.RemoveAt(dataGridView1.Rows[i].Index);
cmd.ExecuteNonQuery();
}
i++;
}while(i< dataGridView1.RowCount);
}
}
private void Form1_Load(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd = con.CreateCommand();
cmd.CommandText = "select * from Employees";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
column.HeaderText = "Select";
column.Name = "select";
column.ReadOnly = false;
dataGridView1.Columns.Add(column);
dataGridView1.AllowUserToAddRows = false; }
} private void bttnDelete_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd = con.CreateCommand();
cmd.CommandText = "delete from Employees where EmployeesID=@id";
DataTable table = (DataTable)dataGridView1.DataSource;
int i=0;
MessageBox.Show(dataGridView1.RowCount.ToString()); do
{
string setl = dataGridView1.Rows[i].Cells["select"].EditedFormattedValue.ToString();
if (setl == "True")
{
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("id", dataGridView1.Rows[i].Cells[0].Value));
table.Rows.RemoveAt(dataGridView1.Rows[i].Index);
cmd.ExecuteNonQuery();
}
i++;
}while(i< dataGridView1.RowCount);
}
}
而你的while条件又是比的(i< dataGridView1.RowCount);
所以出现你说的情况。例如本来datagridview选中4行,说明你要删除4行。但是实际情况是,i=0,删除1行,现在dgv只有3行了,你让i自增变成1,1<3,
执行循环,删除1行,datagridview现在变成2行,i自增成2,
再循环已经不符合条件了,因为现在i=2,datagridview的rowcount也是2
所以导致每次循环都是只有一半。解决方法是,两个变量都在变,你要控制一个,让另一个变而不是两个一起变。你可以把选中的id
放到一个数组里,然后从这个数组依次取,然后删除,而不是用这个循环。
int j=dataGridView1.RowCount;
do
{ string setl = dataGridView1.Rows[i].Cells["select"].EditedFormattedValue.ToString();
if (setl == "True")
{
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("id", dataGridView1.Rows[i].Cells[0].Value));
table.Rows.RemoveAt(dataGridView1.Rows[i].Index);
cmd.ExecuteNonQuery();
}
i++;
}while(i< j);
}
}
i要小于原来的dataGridview1的行数,否则,删除的行数(i)大于剩下的行数(RowCount-i)条件就不成立了……
do
{ string setl = dataGridView1.Rows[i].Cells["select"].EditedFormattedValue.ToString();
if (setl == "True")
{
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("id", dataGridView1.Rows[i].Cells[0].Value));
table.Rows.RemoveAt(dataGridView1.Rows[i].Index);
cmd.ExecuteNonQuery();
}
i++;
}while(i< j);
}
}