简体   繁体   English

如果是整数,如何将DataGridView中的单元格值转换为整数,如果是字符串,如何将其转换为字符串?

[英]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. 我有一个名为“ dgvDynamic”的DataGridview,它可以将来自MS Access DB的数据显示到DataGridView中。 I want to show the value of the selected cell, into a textbox called "txtSelectedCell". 我想在一个名为“ 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 我应该使用“ ClickEvent”,然后更改语句,而不是使用“ CellContent”事件

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

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

Thanks @Codex for solving the problem for me. 感谢@Codex为我解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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