简体   繁体   English

使用表单 C# 向数据网格视图添加行

[英]Adding rows to a data grid view using a form C#

How can I add rows to a data grid view using a separate form?如何使用单独的表单向数据网格视图添加行? When I'm trying to pass it like this当我试图像这样传递它时

        private void btnDodajPrzychod_Click(object sender, EventArgs e)
    {
        formMain.gridPrzychody.Rows.Add("123");
        this.Close();
    }

it says that an object reference is required for the non-static field method or property "formMain.gridPrzychody".它表示非静态字段方法或属性“formMain.gridPrzychody”需要对象引用。

How can I fix it?我该如何解决? Thanks谢谢

When you calling the new Form, you must pass a handler to access the gridView from previous form.当您调用新的 Form 时,您必须传递一个处理程序以从以前的表单访问 gridView。 the handler could be the first form object or the gridview object.处理程序可以是第一个表单对象或 gridview 对象。 for example:例如:

FormNew fm = new FormNew(this.gridPrzychody);

then in the new form get and store the handler.然后在新表单中获取并存储处理程序。 when ever you want, you can use it then.只要你想,你就可以使用它。

GridView gridView;
public FormNew(GridView gridView)
{
   this.gridView = gridView;
}

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

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