datagridview Form2填充ButtonClick

问题描述:

我有两种形式。 form1上的 是一个datagridview和按钮。 On ButtonClick事件中,datagridview正在验证中,如果为null或为空,则返回,否则应打开form2。 (这是语法) 但是当应用程序运行,并且form2将被打开时,它会填充form2的大约15个。 请我如何阻止人口 这里是代码datagridview Form2填充ButtonClick

private void button3_Click(object sender, EventArgs e) 
     { 
      this.dataGridView1.ClearSelection(); 
        foreach (DataGridViewRow row in dataGridView1.Rows) 
        { 
         if (row.IsNewRow) { return; } 
         foreach (DataGridViewCell cell in row.Cells) 
         { 
          // Validating cell value 
          var regexItem = new System.Text.RegularExpressions.Regex("^[a-zA-Z0-9 ]$"); 
          if (cell.Value == null || !regexItem.IsMatch(cell.Value.ToString())) 
          { 
           cell.Style.BackColor = Color.Red; 
          } 

         } 
        } 
       this.Hide(); 
       resultVV f2 = new resultVV(); 
       f2.Show(); 
     } 

您可以使用OwnedForms

private void button3_Click(object sender, EventArgs e) 
     { 
      this.dataGridView1.ClearSelection(); 
        foreach (DataGridViewRow row in dataGridView1.Rows) 
        { 
         if (row.IsNewRow || (this.OwnedForms!=null && this.OwnedForms.Count>=15) { return; } 
         foreach (DataGridViewCell cell in row.Cells) 
         { 
          // Validating cell value 
          var regexItem = new System.Text.RegularExpressions.Regex("^[a-zA-Z0-9 ]$"); 
          if (cell.Value == null || !regexItem.IsMatch(cell.Value.ToString())) 
          { 
           cell.Style.BackColor = Color.Red; 
          } 

         } 
        } 
       this.Hide(); 
       resultVV f2 = new resultVV(); 
f2.Owner = this; 
       f2.Show(); 
     } 
+0

嗨@Evgeny!在堆栈溢出中,我们倾向于认为答案包括解释,而不仅仅是代码。你可能会编辑你的答案,包括你的代码在做什么的简要解释?谢谢,欢迎来到SO! –