简体   繁体   中英

How do I capture data from a single cell in `DataGridView`

I am trying to get the value from the ID field in a DataGridView . I had the Column labeled Contact_ID set to visible = false and working fine, I could get the data from the column.

Today I am getting an error message Unhandled acceptation of type and a message Column named Contact_ID cannot be found

I have made the Contact_ID visible = true and the width of the column = 2 so that you cant really see it

Here is the 2 lines of code that I am using. The row index is being captured just fine.

DataGridViewRow row = customer_ContactsDataGridView.CurrentCell.OwningRow;
string contact_ID = row.Cells["Contact_ID"].Value.ToString();

Any help will be gratefully appreciated.

With the help from Alex Bell, here are the lines of code that I ended up with.

DataGridViewRow row = customer_ContactsDataGridView.CurrentCell.OwningRow;
string contact_ID = row.Cells[0].Value.ToString();

it is easier to get the data by using it's numeric cell location in a DataGridView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM