0

I have a very simple piece of code that is throwing an unexpected error and I am struggling to understand what could possibly be wrong.

private void dataNames_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
{   
    DataGridViewRow row = dataNames.CurrentCell.OwningRow;
    string val = row.Cells["Name"].Value.ToString();
    txtName.Text = val;
}

The Object dataNames is a DataGridView with only a single Column in it called Names.

When I double click a Name from the Grid I want it to populate the Textbox (txtName)

I fail to see how a Textbox can produce an 'Object not set . . ' error when all I am doing is posting a value to its Text property. When I step through the string 'val' does pick up the correct value from the DataGridView.

2
  • 5
    Your code suggests the column is "Name" but your description suggests it is "Names" Commented Jan 2, 2014 at 11:58
  • Where "txtName" textbox is exactly located ? Inside the grid or outside the grid ? Commented Jan 2, 2014 at 12:05

1 Answer 1

1

Ironically, having spent a good while unable to find the problem, I go and spot it straight after posting the question.

The error is earlier in the Form_Load event, which calls a custom ClearForm method, which blanks all text fields on the form. However I had done the following :-

txtTitle.Text = null;
txtAge.Text = null;
txtName = null;

Hence I set the textbox itself = null, rather than its text property.

Sometimes the simplest of typos produces the most puzzling of errors!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.