简体   繁体   中英

How do I retrieve data from cell of selected row in datagridview?

I have a datagridview with 7 columns, column 0 and 1 are not visible. Column 0 is the ID column. When I select a row I would like to get the value in Column 0 . Once I have the value, I can then delete the row from the data table . Deleting the row from the DataGridView is not a problem.

This line returns the row index with no problem;

int rowIndex = customer_Ship_ContactsDataGridView.SelectedRows[0].Index;

Then with all my research it would appear that this line would get the data from column 0 of the selected row . But it does not. I get an error that states "When converting a string to DateTime, Parse the string to take the date before......"

int contact_ID = int.Parse(customer_Ship_ContactsDataGridView[0, rowIndex].Value.ToString());

Any help with teaching me how to get the value of a column toString from the selected row of a DataGridView would be greatly appreciated.

I have found my answer in another post Get value in specific column in DataGridView . I am not sure why I didn't find this last night except that today I simplified my search and came across this post. Sorry for any trouble I may have caused, I really do like using this site for help.

DataGridViewRow row = customer_Ship_ContactsDataGridView.CurrentCell.OwningRow;

string contact_ID = row.Cells["Contact_ID"].Value.ToString();

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