Capture the value of a cell in C# DataGridView

Good morning experts,

I am trying to capture the value of a cell in a DatGridView, by placing that value in a string variable and from there display it in a text box. I thought I could use the cell enter event to achieve that. Here is the code I used:

        int myRow, myColumn;
        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            myRow = e.RowIndex;
            myColumn = e.ColumnIndex;
            string myVal = dataGridView1[myColumn, myRow].Value.ToString();
            this.textBox1.Text = myVal;
        }

The above produces the following error: Object reference not set to an instance of an object.
So I tried the following instead:

 int myRow, myColumn;
        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView myGrid = new DataGridView();
            dataGridView1 = myGrid;
            myRow = e.RowIndex;
            myColumn = e.ColumnIndex;
            string myVal = dataGridView1[myColumn, myRow].Value.ToString();
            this.textBox1.Text = myVal;
        }

The above code produces  index out of range error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
adamtraskAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
have you tried:

string myVal = dataGridView1.Rows[myRow].Cells[myColumn].Value.ToString();

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
and BTW, your second example won't work for sure because dataGridView1 is set to myGrid which is a brand new (empty) DataGridView!
0
adamtraskAuthor Commented:
In the top code I substituted my line:
string myVal = dataGridView1[myColumn, myRow].Value.ToString();

With yours:

string myVal = dataGridView1.Rows[myRow].Cells[myColumn].Value.ToString();

But got the Object reference not set to an instance of an object error.

So, I corrected the second code to:

 int myRow, myColumn;
        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView myGrid = new DataGridView();
           myGrid= dataGridView1;
            myRow = e.RowIndex;
            myColumn = e.ColumnIndex;
           string myVal = myGrid.Rows[myRow].Cells[myColumn].Value.ToString();
            this.textBox1.Text = myVal;
        }
And still got the very same error:  Object reference not set to an instance of an object.
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Éric MoreauSenior .Net ConsultantCommented:
the second one won't work, see explanation here above.

for the first code snippet, is dataGridView1 really is an object on that form? Do you have something in that cell?
0
Éric MoreauSenior .Net ConsultantCommented:
Can you try this:

string myVal;
if (dataGridView1.Rows[myRow].Cells[myColumn].Value == null)
    myVal = "empty";
else
   myVal = dataGridView1.Rows[myRow].Cells[myColumn].Value.ToString();

Open in new window

0
adamtraskAuthor Commented:
Éric Moreau: Your code didn't produce any errors but at the same time nothing was produced at all.
I added to your code this line to make the value of the variable visible in a textBox, whether "Empty" or otherwise.

this.textBox1.Text = myVal;

And to answer your question: yes I have a dataGridView1 on the form and it contains two rows of data with several columns in each row - all populated.

However, I reverted to another option which worked fine - I tried the CellContentClick instead of the CellEnter event

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            DataGridView myGrid = new DataGridView();
           myGrid= dataGridView1;
            myRow = e.RowIndex;
            myColumn = e.ColumnIndex;
           string myVal = myGrid.Rows[myRow].Cells[myColumn].Value.ToString();
            this.textBox1.Text = myVal;
       
        }
And to my surprise it is working! But I still need to understand why the CellEnter event didn't respond?
0
Éric MoreauSenior .Net ConsultantCommented:
In the CellEnter event, what are the values of myRow and myColumn? Are they set to the correct values?

your code could be read as (removing unnecessary variables) :
string myVal = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
this.textBox1.Text = myVal;

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
But I would still validate for null because ToString() on a null value is not working
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adamtraskAuthor Commented:
I will use the null validation. You are absolutely right. Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.