dinotom
dinotom

Reputation: 5162

datagridview formatting not applying

In my form load event I am setting the defaultcellstyle formats. They are not taking hold anyone know why? None of the formatting that is expected in the code after I bind the datatable to the grid is getting done even though the code steps through it

Private Sub frmADRORD_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'wire the delegate function for incoming updates
    AddHandler m_helper.DataUpdate, AddressOf UpdateGridInvoker
    'bind the visual grid with the binding source
    Me.datagridADRORD.DataSource = dsGridView

    'get data from helper class
    m_dt = m_helper.GetTable()

    'bind the binding source with datatable
    Me.datagridADRORD.DataSource = m_dt

    **'after data loaded, auto resize columns and format
    Me.datagridADRORD.AutoResizeColumn(DataGridViewAutoSizeColumnMode.AllCellsExceptHeader)
    With Me.datagridADRORD.ColumnHeadersDefaultCellStyle
        .BackColor = Color.Gold
        .Alignment = DataGridViewContentAlignment.MiddleCenter
        .WrapMode = DataGridViewTriState.True
        .Font = New Font(Control.DefaultFont, FontStyle.Bold)
    End With
    Me.datagridADRORD.Columns("ADR Price").DefaultCellStyle.Format = "##.##"
    For Each row As DataGridViewRow In Me.datagridADRORD.Rows
        row.Cells("ORD Price").DataGridView.DefaultCellStyle.FormatProvider = Globalization.CultureInfo.GetCultureInfo(m_helper.CurrCultureInfoStr(row.Cells("Currency").Value))
    Next
    Me.datagridADRORD.Columns("Currency Price").DefaultCellStyle.Format = "##.####"
    Me.datagridADRORD.Columns("Difference").DefaultCellStyle.Format = "##.##"**
End Sub

Upvotes: 0

Views: 6853

Answers (1)

Chuck Wilbur
Chuck Wilbur

Reputation: 2620

You have to set the ValueType of the column, and "##.##" doesn't seem to work (in C# anyway).

This works in C#:

        this.dataGridView1.Columns["Column3"].ValueType = typeof(Double);
        this.dataGridView1.Columns["Column3"].DefaultCellStyle.Format = "N2";

Also the data being bound has to actually be of some numeric type (I presume it is, but can't be sure from your code snippet).

You'll have to translate typeof() to equivalent VB.NET, and I don't know whether the difference between "##.##" and "N2" is a control thing (in which case you'll need to change it) or a language thing (in which case you won't).

Upvotes: 2

Related Questions