2017-10-21 138 views
-2

我有兩種形式。 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(); 
     } 

回答

0

您可以使用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! –