首页 > 解决方案 > 将 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)

如何解决这个问题?

标签: vb.netdatagridview

解决方案


您在此处指定列单元格的格式:

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))

推荐阅读