如何在vb.net中的数据表编辑时和编辑后设置datagridview和datarow单元格格式

问题描述 投票:0回答:1

我正在尝试在 vb.net 中的数据表编辑时和编辑后设置 datagridview 和 datarow 单元格格式。

Price 列在编辑后是否仍可以使用 string.empty 以便与该格式匹配?

也许我的代码有问题,请指导我。

谢谢

Dim dt As New DataTable()
    Private Sub loadfile()
        Try
            Dim extensions() As String = {".jpg", ".png", ".jpeg", ".gif", ".tiff", ".bmp"}
            Dim filepath As String() = Directory.GetFiles(TextBox1.Text, "*.*").Where(Function(f) extensions.Contains(System.IO.Path.GetExtension(f).ToLower())).ToArray()
            dt.Columns.Add("Filename")
            dt.Columns.Add("Fullname")
            dt.Columns.Add("Price")
            Dim value As Integer = 0
            For item As Integer = 0 To filepath.Length - 1
                Dim file As New FileInfo(filepath(item))
                Dim dr As DataRow = dt.NewRow()
                dr(0) = file.Name
                dr(1) = file.FullName
                'dr(2) = value.ToString("c", CultureInfo.CreateSpecificCulture("id-ID"))
                dr(2) = String.Empty
                dt.Rows.Add(dr)
            Next
            DataGridView1.DataSource = dt
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        FolderBrowserDialog1.ShowDialog()
        TextBox1.Text = FolderBrowserDialog1.SelectedPath.ToString()
        loadfile()
    End Sub
    Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        If DataGridView1.Columns(e.ColumnIndex) Is DataGridView1.Columns("Price") Then
            Dim price As Double = Convert.ToDouble(DataGridView1.Rows(e.RowIndex).Cells("Price").Value)
            DataGridView1.Columns("Price").DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("id-ID")
            DataGridView1.Columns("Price").DefaultCellStyle.Format = String.Format("c")
        End If
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Fullname.FillWeight = CSng("300")
        DataGridView1.Columns("Price").DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("id-ID")
        DataGridView1.Columns("Price").DefaultCellStyle.Format = String.Format("c")
        DataGridView1.Columns("Price").ValueType = GetType(Integer)
    End Sub 

上面代码的结果

Filename  Fullname              Price 
A.png     C:\Input\A.png    1000
B.png     C:\Input\B.png    2000

期望的结果1

Filename  Fullname              Price 
A.png     C:\Input\A.png    Rp1.000,00
B.png     C:\Input\B.png    Rp2.000,00

期望的结果2

Filename  Fullname              Price 
A.png     C:\Input\A.png    Rp1.000
B.png     C:\Input\B.png    Rp2.000
vb.net datatable datagridview string-formatting datarow
1个回答
0
投票

根据我对你问题的理解,这应该可行

Dim FormattedNumber As String = $"Rp{DataGridView1.CurrentRow.Index}{DataGridView1.CurrentRow.Cells(2).Value:N2}"

Dim FormattedNumber As String = $"Rp{DataGridView1.CurrentRow.Index}{FormatNumber(DataGridView1.CurrentRow.Cells(2).Value,2)}"

查看将其应用到 Datagrid End Edit 事件以及列索引 = 2 的位置

© www.soinside.com 2019 - 2024. All rights reserved.