I have already visited this question on StackOverflow but I didn't get any solution to my problem.
In my Windows Form Application, I have included a feature which allows a user to double click a row in the DataGridView and the values from that specific row are shown in text boxes in a child form. The user can now edit these values i.e. can edit quantity etc.
Now, I want to assign this value (the edited quantity) back to that specific cell of the current row of the same DataGridView. I have these values from the child form back on my main form but as I am re-assigning this edited quantity value to the DataGridView in a public method, so I am getting a Nulled Reference Exception on the DataGridView.
Here is my code where I am getting the NulledReferenceException.
//A public method to which I have passed the customQuantity from my child form
public void GetSoldItemQuantity(string customQuantity)
{
//assign this customQuantity to an int variable
int globalQuantity = Convert.ToInt32(customQuantity);
if (globalQuantity > 1)
{
//dgvAllSoldItems is the DataGridView from where I have passed
//the currentRow data to the child form. At this stage,
//I have the updated value returned from my child form and is
//currently stored in the globalQuantity variable but I cannot assign it
//back to the grid.
dgvAllSoldItems.CurrentRow.Cells[1].Value = globalQuantity;
}
}
And here is the button code on my child form which passes the custom quantity value to the above public method on the parent form.
private void btnProceedWithNewItemQuantity_Click(object sender, EventArgs e)
{
string newItemQuantity = txtChooseSaleQuantity.Text.Trim();
frmNewSale parentSale = this.Parent as frmNewSale();//now I am getting error the null reference exception on this line in debugger.
parentSale.GetSoldItemQuantity(newItemQuantity);
this.Close();
}
Although I was creating a new instance of the parent class on the button click event on child form, which was not the right approach. So, I used this.Parent instead, but now I am getting the same exception on this line.