日期:2014-05-18 浏览次数:20913 次
for (int i = 0; i < dgv_Data2.Rows.Count - 1; i++) { for (int j = 1; j < dgv_Data2.Rows.Count - 1; j++) { if ((i + j) <= dgv_Data2.Rows.Count) { string value1 = Convert.ToString(dgv_Data2.Rows[i].Cells[2].Value); string value2 = Convert.ToString(dgv_Data2.Rows[i + j].Cells[2].Value); if (value1 == value2) { MessageBox.Show("此货物已存在!"); } else { string name = dgv_Data2.Rows[i].Cells[2].Value.ToString(); string sql = "select * from MIS_MaterielRegiser where MR_Name='" + name + "'"; ocmd = new OleDbCommand(sql, ocnn); OleDbDataReader reader = ocmd.ExecuteReader(); reader.Read(); dgv_Data2.Rows[i].Cells[1].Value = reader["MR_Number"].ToString(); dgv_Data2.Rows[i].Cells[3].Value = reader["MR_Model"].ToString(); dgv_Data2.Rows[i].Cells[4].Value = reader["MR_Unit"].ToString(); } } } }
int length = dgv_Data2.Rows.Count - 1; for (int i = 0; i <length ; i++) { for (int j = length; j > i; j--) { string value1 = Convert.ToString(dgv_Data2.Rows[j].Cells[2].Value); string value2 = Convert.ToString(dgv_Data2.Rows[j-1].Cells[2].Value); if (value1 == value2) { MessageBox.Show("此货物已存在!"); } else { string name = dgv_Data2.Rows[i].Cells[2].Value.ToString(); string sql = "select * from MIS_MaterielRegiser where MR_Name='" + name + "'"; ocmd = new OleDbCommand(sql, ocnn); OleDbDataReader reader = ocmd.ExecuteReader(); reader.Read(); dgv_Data2.Rows[i].Cells[1].Value = reader["MR_Number"].ToString(); dgv_Data2.Rows[i].Cells[3].Value = reader["MR_Model"].ToString(); dgv_Data2.Rows[i].Cells[4].Value = reader["MR_Unit"].ToString(); } } }
------解决方案--------------------
你是要比较所有的行么?
for (int j = 0; j < dgv_Data2.Rows.Count - 1; j++)
{
if (i!=j)
{
string value1 = Convert.ToString(dgv_Data2.Rows[i].Cells[2].Value);
string value2 = Convert.ToString(dgv_Data2.Rows[j].Cells[2].Value);