简体   繁体   中英

How do you convert a cell value in a DataGridView into a integer if it's an integer, and a string when it's a string?

I have a DataGridview called "dgvDynamic" which displays data from a MS Access DB into a DataGridView. I want to show the value of the selected cell, into a textbox called "txtSelectedCell".

However, it will only show data of a string datatype in the textbox, not integers. How can I display both strings and integers in the textbox? Thank you. Here is the code:

Private Sub dgvDynamic_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDynamic.CellContentClick

        txtSelectedCell.Text = dgvDynamic.SelectedCells(0).Value.ToString

End Sub

Turns out I used the wrong event. Instead of using the "CellContent" event I should've used the "ClickEvent" and then changing the statement

From: txtSelectedCell.Text = dgvDynamic.SelectedCells(0).Value.ToString

To: txtSelectedCell.Text = dgvDynamic.CurrentCell.Value.ToString()

Thanks @Codex for solving the problem for me.

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