Display Data with Format in Datagridview

2019-01-12 13:09发布

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

2条回答
再贱就再见
2楼-- · 2019-01-12 13:29

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
查看更多
等我变得足够好
3楼-- · 2019-01-12 13:38

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.

查看更多
登录 后发表回答