在vb.net中将datagridview值传递到具有格式的消息框
我想将数据从datagridview中的一个格式化列传递到消息框,但问题是数据已传递,但格式未包含在内。我的意思是,当我传递数据时,我想显示的列是一个数字列,只传递数字。我希望包括逗号和句点 图为: 下面是消息框结果: 如您所见,没有逗号和句点。我想包括逗号和句点 以下是在vb.net中将datagridview值传递到具有格式的消息框,vb.net,datagridview,Vb.net,Datagridview,我想将数据从datagridview中的一个格式化列传递到消息框,但问题是数据已传递,但格式未包含在内。我的意思是,当我传递数据时,我想显示的列是一个数字列,只传递数字。我希望包括逗号和句点 图为: 下面是消息框结果: 如您所见,没有逗号和句点。我想包括逗号和句点 以下是datagridview\u单元格格式设置: Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Window
datagridview\u单元格格式设置
:
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.Show(DataGridView1.Rows(0).Cells(7).Value)
如何解决此问题?您在此处指定列单元格的格式:
DataGridView1.Columns(7).DefaultCellStyle.Format = "N2"
在将值转换为字符串
时,您也需要提供相同的格式说明符,例如
MessageBox.Show(DataGridView1.Rows(0).Cells(7).Value.ToString(DataGridView1.Columns(7).DefaultCellStyle.Format))
如果要对进行严格的选项,则需要执行以下操作:
MessageBox.Show(CDbl(DataGridView1.Rows(0).Cells(7).Value).ToString(DataGridView1.Columns(7).DefaultCellStyle.Format))
尝试对数值使用字符串格式:
MessageBox.Show(string.Format(“{0:N2}”、DataGridView1.Rows(0)、Cells(7.Value))
或显式设置为MessageBox.Show(string.Format(“{0:N2}”、DataGridView1.Rows(0)、Cells(7.Value))
。旁注:这不是处理CellFormatting
事件的正确方法。有关示例,请参见。