日期:2014-05-18  浏览次数:20913 次

winform for循环
C# code

       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();
                        }
                    } 
                    
                }
            }
dgv_Data2是datagridview 我是想判断单元格的内容是否跟前面输入的相同,不过这样写在第一行和第二行输入值后。相应的单元不会自动填充值。但是如果第三行输入值后就会全部自动填好!我不知道逻辑错在哪,请大家指正!

------解决方案--------------------
C# code

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);