vb.net - 将 datagridview 值传递给带有 vb.net 格式的消息框
问题描述
我想将数据从 datagridview 中的一个格式化列传递到消息框,但问题是数据已传递但不包括格式。我的意思是当我传递数据时我想要显示的列是一个数字列,只有数字被传递。我希望包含逗号和句点。
这是图像:
这是消息框的结果:
如您所见,没有逗号和句号。我想包括逗号和句点。
这是datagridview_cellformatting
:
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If (e.ColumnIndex = 7) Then
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).ValueType = GetType(Double)
DataGridView1.Columns(7).DefaultCellStyle.Format = "N2"
End If
If DataGridView1.Rows(e.RowIndex).Cells(7).Value Is DBNull.Value Then
DataGridView1.Rows(e.RowIndex).Cells(7).Value = "0.00"
End If
End Sub
这是MessageBox
:
MessageBox.Show(DataGridView1.Rows(0).Cells(7).Value)
如何解决这个问题?
解决方案
您在此处指定列单元格的格式:
DataGridView1.Columns(7).DefaultCellStyle.Format = "N2"
String
在将值转换为其他任何地方时,您也需要提供相同的格式说明符,例如
MessageBox.Show(DataGridView1.Rows(0).Cells(7).Value.ToString(DataGridView1.Columns(7).DefaultCellStyle.Format))
如果你有Option Strict On
,你总是应该的,那么你需要这样做:
MessageBox.Show(CDbl(DataGridView1.Rows(0).Cells(7).Value).ToString(DataGridView1.Columns(7).DefaultCellStyle.Format))
推荐阅读
- c# - 如何使用 selenium WebDriver 更改转换属性
- c++ - 如何在现代 GNU/Linux 发行版上运行旧的二进制文件?
- sql - 将多对多关联的属性定义为唯一的
- outlook - Outlook 兑换:在 VBScript 中从 GetMessageFromMsgFile 释放 RDOMail
- java - 未创建房间数据库
- python - 为什么我得到错误的预测值 python-weka 包装器
- javascript - 如果数组对象中的值相同,则用逗号分隔值连接
- amazon-web-services - 将 API 网关与 SAM 模板集成以具有自定义名称和阶段
- c# - 插入实体时实体框架重复项
- javascript - 如何仅使用香草 javascript 将表情符号按钮添加到我的网站