简体   繁体   English

通过单击按钮到form2删除数据网格视图行

[英]Deleting data grid view rows by clicking button to form2

I have restaurantSale(form1) and I have supervisorVoidPass (form2) the problem is i cant remove the data ..(The system cant find the object) 我有restaurantSale(form1),我有supervisorVoidPass(form2),问题是我无法删除数据..(系统找不到对象)

in my form(1) is my datagridview. 在我的form(1)中是我的datagridview。

then in my form 2 this is my code which this code use for deleting datagridview per row 然后在我的表格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");
                }

            }
        }

this line 这条线

restaurantSale rs = new restaurantSale();

creates a new instance of a form, not the one which is already open (and has some data). 创建一个表单的新实例,而不是已经打开(并有一些数据)的表单。

the existing form should be used instead. 应该改用现有表格。 it can be found in Application.OpenForms 可以在Application.OpenForms找到

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

it also makes sense to initialize rs value after password check ( if (voidPass.Text == inpPass) ) 密码检查初始化rs值也很有意义( if (voidPass.Text == inpPass)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM