Display Data with Format in Datagridview

时光怂恿深爱的人放手 提交于 2019-11-26 18:39:43

问题


Lets say you have a column in Datagridview that has Negative and Positive Numbers that act as a data.

How can you display numbers with negatives into Open and Close Parenthesis? For example -5.00 will turn to (5.00)

How can I achied that? TYSM


回答1:


You can set the default format for that column:

DataGridView1.Columns(n).DefaultCellStyle.Format = "#,##0.00;(#,##0.00)"

Some further info on formatting can be found here.




回答2:


Answering to your comments.

Doing it in SQL add to your query something like this:

SELECT IF(VALUE<0, ABS(VALUE), VALUE)

Or

Doing it in vb.net:

Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
    Handles dataGridView1.CellFormatting

    If dataGridView1.Columns(e.ColumnIndex).Name.Equals("YOURCOLUMN") Then
        If CInt(e.Value) < 0 Then
            e.Value = "(" & Math.Abs(e.Value).ToString("N") & ")"
        End If
    End If

End Sub


来源:https://stackoverflow.com/questions/39270205/display-data-with-format-in-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!