C# How to solve "DataGridView.CurrentRow.get returned null"?

27 views Asked by At

In my C# program, I want to change the values in my DataGridView, I'm sure the code is correct, but if I put my code in a public void, it has an error:

System.Windows.Forms.DataGridView.CurrentRow.get returned null.

The problem appears when any DataGridView code inside a void.

My code below:

public void SaveData(int ID, string Guid, string Name, DateTime Birth, string status)
{
    string[] valuess = { Name, Birth.ToString("yyyy/MM/dd"), status };
    for (int i = 0; i < 3; i++)
    {
        dataGridView1.CurrentRow.Cells[i].Value = values[i];
    }
}

How can I solve this problem?

I've tried this code under a button click event, it works normally, but it won't work under a public void.

0

There are 0 answers