简体   繁体   中英

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".

How can I fix it? Thanks

When you calling the new Form, you must pass a handler to access the gridView from previous form. the handler could be the first form object or the gridview object. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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