ds中的表与dgv中的选定单元格比较错误
dataGridView5.Rows[dataGridView5.CurrentCell.RowIndex].Selected = true;SqlConnection thisConnection = new SqlConnection(@"Server=.;database=mystudent;Integrated Security=true;"); //连接数据库
SqlDataAdapter thisAdapter = new SqlDataAdapter("SELECT * FROM TABLE1 order by 学号 asc", thisConnection); //将查询字符串与连接适配
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
DataSet thisDataSet = new DataSet(); //定义一个DataSet
thisAdapter.Fill(thisDataSet, "TABLE1"); //将TABLE1填充到DataSet
for(int j=0;j<thisDataSet.Tables["TABLE1"].Rows.Count;j++)
{
for (int i = 0; i < 5; i++)
{
if (!thisDataSet.Tables["TABLE1"].Rows[j][i].ToString().Trim().Equals(this.dataGridView1.CurrentRow.Cells[this.dataGridView1.CurrentCell.ColumnIndex].Value.ToString().Trim()))
{
thisDataSet.Tables["TABLE1"].Rows[j][i]= this.dataGridView5.CurrentRow.Cells[i].Value.ToString().Trim();
}
}
}
thisAdapter.Update(thisDataSet, "TABLE1");
就看if那里,我跟踪了,每次都执行if里面的赋值语句,本意是想如果dgv里改动的值和ds里表的值不相等时,就赋值给ds对应的单元格,然后更新,不想if语句竟不起效果