Collecting values from datagridview.selectedrows[0].cells
-
I'm trying to collect the values from a datagridview in a child form and place them in textboxes located in it's parent form. I'm using the following statement for one of the values:
txtFirstName.Text = this.finder.dataGridView1.SelectedRows[0].Cells["firstNameDataGridViewTextBoxColumn"].Value.ToString();
As far as I can tell, it should be working. When I run the program, once it gets to this point it throws an error telling me that the values are null. There are no spelling errors. "finder" is the child form. Please help.:confused:
-
I'm trying to collect the values from a datagridview in a child form and place them in textboxes located in it's parent form. I'm using the following statement for one of the values:
txtFirstName.Text = this.finder.dataGridView1.SelectedRows[0].Cells["firstNameDataGridViewTextBoxColumn"].Value.ToString();
As far as I can tell, it should be working. When I run the program, once it gets to this point it throws an error telling me that the values are null. There are no spelling errors. "finder" is the child form. Please help.:confused:
Have you tried determining which part of the statement is null? Are you trying to just get the first selected row and are you sure there is a row selected?
-
Have you tried determining which part of the statement is null? Are you trying to just get the first selected row and are you sure there is a row selected?
Sorry, maybe I'm misunderstanding the error message. It's not exactly telling me the value is null... The message I'm getting is, "Object reference not set to an instance of an object." It's pointed at "firstNameDataGridViewTextBoxColumn". After some testing I'm finding the same error on my search statement to fill my datagridview, now, as well:
this.finder.customerTableAdapter.FillByFirstLastPhoneNo(this.finder.edsDBDataSet.Customer, txtLastName.Text, txtFirstName.Text, txtPhone.Text);
If I catch it, however, it continues and fills the datagridview normally, dispite throwing this exception. I'm still pretty green with this, unfortunately. I'm really not quite sure what exactly is going wrong, since it didn't have this problem earlier.
-
Sorry, maybe I'm misunderstanding the error message. It's not exactly telling me the value is null... The message I'm getting is, "Object reference not set to an instance of an object." It's pointed at "firstNameDataGridViewTextBoxColumn". After some testing I'm finding the same error on my search statement to fill my datagridview, now, as well:
this.finder.customerTableAdapter.FillByFirstLastPhoneNo(this.finder.edsDBDataSet.Customer, txtLastName.Text, txtFirstName.Text, txtPhone.Text);
If I catch it, however, it continues and fills the datagridview normally, dispite throwing this exception. I'm still pretty green with this, unfortunately. I'm really not quite sure what exactly is going wrong, since it didn't have this problem earlier.
The error message usually means that one of the objects has not been created. This could be due to the column not existing or rows not being selected. Are you checking that SelectedRows.Count is greater than 0? If there are no explicitly selected rows then this could be the problem.
-
The error message usually means that one of the objects has not been created. This could be due to the column not existing or rows not being selected. Are you checking that SelectedRows.Count is greater than 0? If there are no explicitly selected rows then this could be the problem.
It looks as though the problem lies in my sql query. I'm not sure how it went wrong, since I've been using it successfully for a week. Reconfiguring the query has not helped, either. My query is as follows:
SELECT Address, Customer_ID, First_Name, Last_Name, Phone_No FROM Customer
WHERE (Last_Name LIKE @Last_Name) AND (First_Name LIKE @First_Name) AND (Phone_No LIKE @Phone_No)called by the code I posted previously:
this.finder.customerTableAdapter.FillByFirstLastPhoneNo(this.finder.edsDBDataSet.Customer, txtLastName.Text, txtFirstName.Text, txtPhone.Text);
Thank you for trying to help thus far. I'll keep working on this; maybe someone will have some insight on what's going wrong. :wtf: