如何使datagridview列中的值转换为记帐格式 [英] How to make the values in a datagridview columns to accounting format

查看:38
本文介绍了如何使datagridview列中的值转换为记帐格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个怎么样,如果您愿意的话,您能为我做些解决方案吗?

How about this one, can you do me a little solution for this if you would mind?

我的datagridview列(单位")中带有负值;

I had this datagridview columns("Units") that has Negative Values in it;

Units
      3
     -3
      2
     -3
      3
     -2
Total 8

现在我想在下面将负值转换为(3),

Now i want to convert Negative Values into (3) like this, below;

    Units
      3
     (3)
      2
     (3)
      3
     (2)
Total 8

我该怎么做?非常感谢...

How do i do this? Thanks a lot...

推荐答案

考虑到列类型是字符串类型,一种可能的解决方案是使用 NumberFormat 显示带括号的负数. MSDN FormatNumber

Considering that the column type is a string type, one possible solution is to use a NumberFormat to display negative numbers with parenthesis. MSDN FormatNumber

可以使用 TriState 常量将第四个参数 UseParensForNegativeNumbers 设置为True.

The fourth parameter UseParensForNegativeNumbers can be set to True with the TriState constant.

Dim numberString As String = FormatNumber(number, 0,, TriState.True,)

这应该消除删除负号和添加括号的工作.以下是一种采用数字字符串并返回相同数字字符串的方法,除非该字符串的数字值小于零(0),在这种情况下,该方法将返回去除了负号并添加括号的字符串.如果字符串不是数字,则返回原始字符串.

This should take the work out of removing the negative sign and adding the parenthesis. Below is a method that takes a number string and returns the same number string unless the string’s number value is less than zero (0), in which case will return the string with the negative sign removed and parenthesis added. If the string is not a number, the original string is returned.

Private Function GetAccountingNumber(originalString As String) As String
  If (Not (String.IsNullOrEmpty(originalString))) Then
    Dim number As Integer
    If (Integer.TryParse(originalString, number)) Then
      If (number < 0) Then
        Return FormatNumber(number, 0,, TriState.True,)
      Else
        Return originalString
      End If
    Else
      Return originalString
    End If
  Else
    Return ""
  End If
End Function

鉴于此,您可能想要设置此格式的一个地方是在第3列触发网格 CellValueChanged 事件时,这表示单元格值已更改,并且如果它是负数,您需要在用户离开该单元格后对该单元格进行格式化.网格 CellValueChanged 事件可能对此有所帮助……如果更改的单元格值在第3列中,则我们要设置单元格值的格式.由于我们正在更改单元格的值,因此在设置该值之前,请务必关闭此事件,以免出现再次输入错误.

Given this, one place you may want to set this format is when the grids CellValueChanged event is fired for column 3. This would mean the cells value changed, and if it were a negative number, you would want to format that cell after the user leaves the cell. The grids CellValueChanged event may help for this… if the cell value changed is in column 3 then we want to format the cells value. Since we are changing the cells value, before setting the value, it is prudent to turn off this event to avoid a re-entrant error.

Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
  If (e.RowIndex >= 0 And e.ColumnIndex = 3) Then
    If (DataGridView1.Rows(e.RowIndex).Cells(3).Value IsNot Nothing) Then
      RemoveHandler DataGridView1.CellValueChanged, AddressOf DataGridView1_CellValueChanged
      DataGridView1.Rows(e.RowIndex).Cells(3).Value = GetAccountingNumber(DataGridView1.Rows(e.RowIndex).Cells(3).Value.ToString())
      AddHandler DataGridView1.CellValueChanged, AddressOf DataGridView1_CellValueChanged
    End If
  End If
End Sub

我猜这是您要寻找的.希望对您有所帮助.

I am guessing this is what you are looking for. Hope it helps.

这篇关于如何使datagridview列中的值转换为记帐格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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