vb.net 2010VS datagridview单格格式 [英] vb.net 2010VS datagridview Single cell format

查看:89
本文介绍了vb.net 2010VS datagridview单格格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试格式化单个单元格。我有一个未绑定的datagridview,我想将当前单元格格式化为货币。



 DataGridView1.CurrentCell.Style.Format =   c 

DataGridView1.Item( 2 2 )。Style.Format = c





这些说明都不起作用。无论如何我可以这样做吗?

解决方案

单元格格式不适用于现有值,它会自动更改后续值。我想出了这个;



 私人  Sub  Button2_Click(发件人作为系统。对象,e  As  System.EventArgs)句柄 Button2.Click 

对于 每个单元格作为 DataGridViewCell DataGridView1.SelectedCells
cell.ValueType = GetType (系统。单个
Dim newStyle 作为 DataGridViewCellStyle
newStyle.Format = C
cell.Style.ApplyStyle( newSty le)
下一步

结束 Sub

私有 Sub DataGridView1_CellFormatting(发送者) 作为 对象,e As System.Windows .Forms.DataGridViewCellFormattingEventArgs)句柄 DataGridView1.CellFormatting
如果 IsNothing(e.Value)然后
如果 IsNumeric(e .Value)然后
e.Value =格式( CDbl (e.Value),e .CellStyle.Format)
结束 如果
结束 如果
结束 Sub


试试这段代码让我知道它是否有效。



 私有  Sub  DGVStocksMaster_CellFormatting(发件人作为 < span class =code-keyword> Object ,e  As  System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles  DGVStocksMaster.CellFormatting 
如果 IsNothing(e.Value)然后
如果 e.ColumnIndex()。ToString = 6 IsNumeric(e.Value)然后
e.Value =格式( FormatCurrency(EV alue))' ,e.CellStyle.Format)
结束 如果
结束 如果
结束 Sub


I'm trying to format a single cell. I have a unbound datagridview and I want to format the current cell as currency.

DataGridView1.CurrentCell.Style.Format = "c"

DataGridView1.Item(2, 2).Style.Format = "c"



neither of these instructions work. Is there anyway I can do this?

解决方案

The cell formatting does not apply to the existing value, it will change on subsequent value changes automatically. I have come up with this;

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

    For Each cell As DataGridViewCell In DataGridView1.SelectedCells
        cell.ValueType = GetType(System.Single)
        Dim newStyle As New DataGridViewCellStyle
        newStyle.Format = "C"
        cell.Style.ApplyStyle(newStyle)
    Next

End Sub

Private Sub DataGridView1_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    If Not IsNothing(e.Value) Then
        If IsNumeric(e.Value) Then
            e.Value = Format(CDbl(e.Value), e.CellStyle.Format)
        End If
    End If
End Sub


Try this code let me know if it works.

Private Sub DGVStocksMaster_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DGVStocksMaster.CellFormatting
    If Not IsNothing(e.Value) Then
        If e.ColumnIndex().ToString = 6 And IsNumeric(e.Value) Then
            e.Value = Format(FormatCurrency(e.Value)) ', e.CellStyle.Format)
        End If
    End If
End Sub


这篇关于vb.net 2010VS datagridview单格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆