【问题标题】:Deleting data grid view rows by clicking button to form2通过单击按钮到form2删除数据网格视图行
【发布时间】:2018-01-23 13:30:56
【问题描述】:

我有 restaurantSale(form1) 和 supervisorVoidPass (form2) 问题是我无法删除数据..(系统找不到对象)

以我的形式(1) 是我的数据网格视图。

那么 在我的表格 2 中,这是我的代码,该代码用于删除每行的 datagridview

 private void button8_Click(object sender, EventArgs e)
        {
            restaurantSale rs = new restaurantSale();
            string inpPass = "1234";
            if (voidPass.Text == inpPass)
            {

                MessageBox.Show("Void Success");
                foreach (DataGridViewRow row in rs.receiptGrid.SelectedRows)
                {
                    rs.receiptGrid.Rows.RemoveAt(row.Index);
                    rs.ShowDialog();
                    MessageBox.Show("Void Records");
                }

            }
        }

【问题讨论】:

  • 那么您面临的问题是什么?
  • 那是因为您从 buttonclick 创建了一个新的 form1,然后您使用了这个新的 form1,它当然在 datagridview 中没有数据。不要创建新的表单1,而是使用现有的表单

标签: c# winforms datagridview click


【解决方案1】:

这一行

restaurantSale rs = new restaurantSale();

创建一个新的表单实例,而不是已经打开的(并且有一些数据)。

应该使用现有的表格。可以在Application.OpenForms找到它

restaurantSale rs = Application.OpenForms.OfType<restaurantSale>().FirstOrDefault();
if (rs == null) return;

在密码检查(if (voidPass.Text == inpPass))之后初始化rs值也很有意义(if (voidPass.Text == inpPass)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2021-12-01
    • 2011-04-29
    相关资源
    最近更新 更多