简体   繁体   中英

Call another class' method which order itself' s datagridview

I have 2 forms that 1st form will call 2nd form's method and this method will add new row into datagridview on 2nd form, like ...

//frmChild1
private void updateDg()
{
frmChild2 _frmChild2 = new frmChild2(this);
_frmChild2.InsertDataGridview1("1,2,3,4","A^BB^CCC^DDDD");
}

//frmChild2
private frmChild1 _frmChild1;
public frmChild2(frmChild1 _frm)
{
this._frmChild1 = _frm;
}

public InsertDataGridview1(string str1, string str2)
{
string[] arrParam = {"","","",""};
//Combind str1 and str2 into arrParam
DataGridView1.Rows.Add(arrParam);
}

From my example, DataGridview1 is null. I don't know what I miss. Please give me advise.

By the way the 1st 2 rows of this datagridview already added, I use them as header by order from itself's form. So it should not be null.

You've need to actually show the form:

private void updateDg()
{
  frmChild2 _frmChild2 = new frmChild2(this);
  _frmChild2.Show();
  _frmChild2.InsertDataGridview1("1,2,3,4","A^BB^CCC^DDDD");
}

This way, InitializeComponent will be called to instantiate your UI controls.

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