【问题标题】:datagridview Form2 Populates on ButtonClick按钮单击时填充的 datagridview 表单 2
【发布时间】:2017-10-21 18:55:41
【问题描述】:

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

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

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    您可以使用 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();  
           }  
    

    【讨论】:

    • 嗨@Evgeny!在 Stack Overflow 上,我们倾向于让答案包括解释,而不仅仅是代码。您能否编辑您的答案以包括对您的代码在做什么的简要说明?谢谢,欢迎来到 SO!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    相关资源
    最近更新 更多