datagridview格式化不适用

时间:2012-02-11 09:28:55

标签: vb.net datagridview

在我的表单加载事件中,我正在设置defaultcellstyle格式。他们没有抓住任何人知道为什么?在将数据表绑定到网格后,代码中预期的格式化都没有完成,即使代码逐步执行它

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

1 个答案:

答案 0 :(得分:2)

您必须设置列的ValueType和" ##。##"似乎无法工作(无论如何都是C#。)

这适用于C#:

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

绑定的数据实际上必须是某种数字类型(我认为它是,但不能从您的代码片段中确定)。

您必须将typeof()翻译成等效的VB.NET,而且我不知道" ##。##"之间是否存在差异。和" N2"是控件(在这种情况下你需要改变它)或语言事物(在这种情况下你赢了)。