Display Data with Format in Datagridview

2019-01-12 12:58发布

问题:

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