简体   繁体   中英

c# datagridview Object reference not set to an instance of an object

I'm taking data entered by the user and trying to add them to a datagridview in another form that I created, however, I'm having an

Object reference not set to an instance of an object. when I try to add my first row,

this is the code below:

    monthTrans mt = new monthTrans();
    private void completeBtn_Click(object sender, EventArgs e)
    {
        var todayDate = DateTime.Now;
        //MessageBox.Show(todayDate.ToString());
        var transType = typeLabel.Text;
        var currencySelected = CurrencyList.Text;
        var amount = AmountText.Text;
        var currencyPrice = currencyPriceText.Text;
        var total = totalAmountText.Text;

        var monthlyData = mt.myData;

        monthlyData.Rows.Add(todayDate, transType, currencySelected, amount, currencyPrice, total);

        //open a new window
        mt.Parent = Parent;
        mt.StartPosition = FormStartPosition.CenterParent;
        if (mt.IsDisposed)
              mt = new monthTrans();
        mt.Show();
    }

the error appears on "monthlyData.Rows.Add......."

btw, I added in my datagridview form this code below to access it outside that form:

    public DataGridView myData
    {
        get;
        set;
    }

thanks for your help

try:

private DataGridView _mydata = new DataGridView();
public DataGridView myData
{
    get { return _mydata; }
    set { _mydata = value; } 
}

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