【问题标题】:Prevent blank datarow being inserted to datagridview防止将空白数据行插入到 datagridview
【发布时间】:2014-06-09 21:34:16
【问题描述】:

我有一个带有文本框的表单,可以将数据插入到 datagridview 中。当我输入数据时,它成功地将其输入到 datagridview 中,这很好。但是,如果我在不输入数据的情况下关闭表单,它会在 datagridview 中插入一个空白行,然后在其下方开始一个新行以准备接收更多数据。如何防止它插入空白行?

public partial class newquoteForm : Form
{ 
    public newquoteForm()
    {
        InitializeComponent();
    }
    DataTable dt = new DataTable();
    public void newquoteForm_Load(object sender, EventArgs e)
    {
       DataRow dr;
        dt.Columns.Add("Item Name");
        dt.Columns.Add("Item Description");
        dt.Columns.Add("Retail Price");
        dt.Columns.Add("Cost Price");
        dt.Columns.Add("In Stock");
        dt.Columns.Add("On Jobs");
        dr = dt.NewRow();
        dataGridView1.DataSource = dt;       
    }

    public void addBTN_Click(object sender, EventArgs e)
    {
        additemForm additemForm = new additemForm();
        additemForm.ShowDialog();
        dt.Rows.Add(additemForm.strItem, additemForm.strDesc, additemForm.strRetail); // some methods are missing, Don't worry about it.
        dataGridView1.DataSource = dt;
    }
}

【问题讨论】:

  • 您能否在 OnClose 或 OnClosing 事件上放置断点,以查看是否正在执行其他会导致此问题的代码,您可能还需要设置 dataGridView1.DataSource = null;在表单关闭事件中..

标签: c# winforms datagridview


【解决方案1】:

看起来您的AddItemForm 具有一些属性,可用于在addBTN_Click 执行时创建新行。您永远不会检查 AddItemForm 是否正确初始化了这些属性。您只需随意添加这些属性的值,即使这些值为空。我会向AddItemForm 添加一个属性,例如IsValid,只有在正确初始化其他属性时才会如此。然后在创建新行之前检查一下。

在您的 AddItemForm 中:

public bool IsValid {
    get {
        return !string.IsNullOrEmpty(txtStrItem.Text) &&
               !string.isNullOrEmpty(your other textboxes)...;
        // I'm just guessing here what controls your form has. you should see the point though
    }
}

然后当你创建新行时:

using (var addItemForm = new AddItemForm()) {
    if (addItemForm.ShowDialog() == DialogResult.OK) {
        if (addItemForm.IsValid) {
            dt.Rows.Add(additemForm.strItem, additemForm.strDesc, additemForm.strRetail);
        }
    }
}    

【讨论】:

  • 除了你犯了一个小错误(addItemForm.IsValid())就像一个方法一样使用,没有问题,只需删除()就可以了。
  • 哦,不错不错。我还对其进行了编辑以使用using 语句来确保它被正确处理。
  • 如中,我打开表单,关闭它,然后重新打开,没有错误消息“无法恢复已处理的对象”(或任何它!)
  • 不完全。在这种情况下,您不会每次尝试恢复已处置的对象,因为您创建了一个新表单并且仅在 using 语句中访问新表单。我不能像 msdn 那样给你很好的解释,所以请查看这些链接:msdn.microsoft.com/en-us/library/yh598w02.aspxmsdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx
  • if (FORMNAME == null || FORMNAME.IsDisposed == true) FORMNAME = new FORMNAME();为我工作
【解决方案2】:

假设我创建了一个带有三个文本框的 additemForm 表单,然后在表单关闭时设置 strItem、strDesc 和 strRetail 属性。我还将设置对话框结果:

 void additemForm_Closing(object sender, CancelEventArgs e)
    {
        strItem = this.textBox1.Text;
        strDesc = this.textBox2.Text;
        strRetail = this.textBox3.Text;

        //You can check anything here
        if (string.IsNullOrEmpty(strItem))
        {
            this.DialogResult = DialogResult.Cancel;
        }
        else
        {
            this.DialogResult = DialogResult.OK;
        }
    }

现在在主窗体中,您可以检查 Dialog 结果然后对其进行操作:

 additemForm additemForm = new additemForm();
        DialogResult dialogResult = additemForm.ShowDialog();
        if (dialogResult == DialogResult.Cancel)
        {
            return;
        }
        dt.Rows.Add(additemForm.strItem, additemForm.strDesc, additemForm.strRetail); // some methods are missing, Don't worry about it.
        dataGridView1.DataSource = dt;

【讨论】:

    猜你喜欢
    • 2012-02-06
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多