none
how to call datagridview's cell formatting event by code? RRS feed

  • Question

  • I want to change cell format run time like ##0.00 to ##0.000 so if value is 123.45

    after change format it should display 123.450

    I will change format depends on other column value.

    Friday, April 13, 2018 11:57 AM

All replies

  • Hello,

    Have you tried setting the format as shown below via Numeric or Custom format? And the type of the cell data is important in that the wrong data type will fail while the right data type will work. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, April 13, 2018 1:02 PM
    Moderator
  • Hi

    I believe you want a cell format to be done according to another cell value. If so,then here is some code to illustrate one column values being set according to another cell of same row.

    Here, the fourth column cell (indexed as 3) has formatting set according to the value in first column (indexed as 0)

    ' Form1 with DataGridView1 (4 default
    ' columns)
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim rand As New Random
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' add some random data rows
        ' to DataGridView1, with forth column
        ' cell formatted according to the
        ' value in first column
        For i As Integer = 10 To 19
          Dim pl As Integer = rand.Next(1, 5)
          Dim ind As Integer = DataGridView1.Rows.Add(pl, i, i + 10, i / 3)
          Dim sp As String = "0." & StrDup(pl, "0")
          DataGridView1(3, ind).Style.Format = sp
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Friday, April 13, 2018 1:07 PM
  • Apparently there is a cell formatting event sub available and the code here How to: Customize Data Formatting in the Windows Forms DataGridView Control is in C# but could be converted to VB.Net using Telerik.

    La vida loca

    Saturday, April 14, 2018 6:30 PM
  • DataGridCelFormating(Nothing,Nothing)
    Be aware you cannot call an event only a method that handles it.

    Success
    Cor

    Sunday, April 15, 2018 8:16 AM
  • Hi Avadh,

    You can also take a look the following code, it formats value in cellformating event.

    Private Sub Frmsum_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Id"
            DataGridView1.Columns(1).Name = "DataFormat"
            DataGridView1.Columns(2).Name = "price"
    
            Dim row As String() = New String() {"1", "1", "12.11"}
            DataGridView1.Rows.Add(row)
            row = New String() {"2", "2", "13.11"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "3", "14.11"}
            DataGridView1.Rows.Add(row)
            row = New String() {"4", "4", "15.11"}
            DataGridView1.Rows.Add(row)
            row = New String() {"5", "5", "16.11"}
            DataGridView1.Rows.Add(row)
            row = New String() {"6", "6", "17.11"}
            DataGridView1.Rows.Add(row)
    
        End Sub
    Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            If DataGridView1.Columns(e.ColumnIndex).Name = "price" Then
                Dim value As Integer = DataGridView1.Rows(e.RowIndex).Cells(1).Value
                Dim sp As String = "##0." & StrDup(value, "0")
                Dim value1 As Decimal = DataGridView1.Rows(e.RowIndex).Cells(2).Value
                e.Value = Format(value1, sp)
    
            End If
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Tuesday, April 17, 2018 9:26 AM
    Monday, April 16, 2018 6:04 AM
    Moderator