none
Adding text to datagridview cell ? RRS feed

  • Question

  • Hallo, 

    I have a datagridview bound to a datatable. I want to include a text (like prefix but not same for all rows) to one of the column in datagridview. This column has a datatype of double. Is there a way to add the text only on the DGV without adding to the datatable? 

    Thanks

     

    Friday, October 4, 2019 11:45 AM

All replies

  • Hi

    You could add/insert a new column into the DataGridView and add your Text to the rows in the new column? That would not effect the DataTable.


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, October 4, 2019 12:09 PM
    Friday, October 4, 2019 12:08 PM
  • Hello,

    Here is pattern that does what you want, if there are additional thoughts like can I also change values outside of the assertion, it depends but with the code below this does solve your current question.

    Public Class Form1
        Private operations As New Operations
        Private bsOrderDetails As New BindingSource
        Private SpecialIndicator As String = " *"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            bsOrderDetails.DataSource = operations.GetOrderDetails()
            DataGridView1.DataSource = bsOrderDetails
        End Sub
        Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) _
            Handles DataGridView1.CellFormatting
    
            If DataGridView1.Columns(e.ColumnIndex).Name.Equals("UnitPrice") Then
                If CDbl(e.Value) > 14 Then
                    e.Value = $"{SpecialIndicator}{e.Value}"
                End If
            End If
        End Sub
        Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) _
            Handles DataGridView1.EditingControlShowing
    
            If DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("UnitPrice").Index Then
                CType(e.Control, TextBox).Text = CType(e.Control, TextBox).Text.Replace(SpecialIndicator, "")
                AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
            End If
    
        End Sub
        Private Sub TextBox_keyPress(sender As Object, e As KeyPressEventArgs)
            If Char.IsDigit(CChar(e.KeyChar)) = False Then
                e.Handled = True
            End If
        End Sub
        Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) _
            Handles DataGridView1.DataError
    
            MessageBox.Show(e.Exception.Message)
            e.Cancel = True
    
        End Sub
    End Class
    
    


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, October 4, 2019 2:03 PM
    Moderator
  • Hello,

    Here is pattern that does what you want, if there are additional thoughts like can I also change values outside of the assertion, it depends but with the code below this does solve your current question.

    Public Class Form1
        Private operations As New Operations
        Private bsOrderDetails As New BindingSource
        Private SpecialIndicator As String = " *"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            bsOrderDetails.DataSource = operations.GetOrderDetails()
            DataGridView1.DataSource = bsOrderDetails
        End Sub
        Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) _
            Handles DataGridView1.CellFormatting
    
            If DataGridView1.Columns(e.ColumnIndex).Name.Equals("UnitPrice") Then
                If CDbl(e.Value) > 14 Then
                    e.Value = $"{SpecialIndicator}{e.Value}"
                End If
            End If
        End Sub
        Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) _
            Handles DataGridView1.EditingControlShowing
    
            If DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("UnitPrice").Index Then
                CType(e.Control, TextBox).Text = CType(e.Control, TextBox).Text.Replace(SpecialIndicator, "")
                AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
            End If
    
        End Sub
        Private Sub TextBox_keyPress(sender As Object, e As KeyPressEventArgs)
            If Char.IsDigit(CChar(e.KeyChar)) = False Then
                e.Handled = True
            End If
        End Sub
        Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) _
            Handles DataGridView1.DataError
    
            MessageBox.Show(e.Exception.Message)
            e.Cancel = True
    
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thanks Karen, It works but i would like to know how do i call the CellFormatting event for every row ? because i change the prefix for every row based on a condition.
    Friday, October 4, 2019 3:33 PM
  • Hello,

    Here is pattern that does what you want, if there are additional thoughts like can I also change values outside of the assertion, it depends but with the code below this does solve your current question.

    Public Class Form1
        Private operations As New Operations
        Private bsOrderDetails As New BindingSource
        Private SpecialIndicator As String = " *"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            bsOrderDetails.DataSource = operations.GetOrderDetails()
            DataGridView1.DataSource = bsOrderDetails
        End Sub
        Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) _
            Handles DataGridView1.CellFormatting
    
            If DataGridView1.Columns(e.ColumnIndex).Name.Equals("UnitPrice") Then
                If CDbl(e.Value) > 14 Then
                    e.Value = $"{SpecialIndicator}{e.Value}"
                End If
            End If
        End Sub
        Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) _
            Handles DataGridView1.EditingControlShowing
    
            If DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("UnitPrice").Index Then
                CType(e.Control, TextBox).Text = CType(e.Control, TextBox).Text.Replace(SpecialIndicator, "")
                AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
            End If
    
        End Sub
        Private Sub TextBox_keyPress(sender As Object, e As KeyPressEventArgs)
            If Char.IsDigit(CChar(e.KeyChar)) = False Then
                e.Handled = True
            End If
        End Sub
        Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) _
            Handles DataGridView1.DataError
    
            MessageBox.Show(e.Exception.Message)
            e.Cancel = True
    
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thanks Karen, It works but i would like to know how do i call the CellFormatting event for every row ? because i change the prefix for every row based on a condition.

    Logic is based not on index rows but by an assertion such as below while the first post I gave did one assertion where you could have done one or more else if's.

    Here I use a select case

    Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) _
        Handles DataGridView1.CellFormatting
    
        If DataGridView1.Columns(e.ColumnIndex).Name.Equals("UnitPrice") Then
            Dim currentUnitPrice = CDbl(e.Value)
            Select Case currentUnitPrice
                Case 1 To 14
                    e.Value = $"+{e.Value}"
                Case 14.25D To 20
                    e.Value = $"*{e.Value}"
                Case > 20
                    e.Value = $"~{e.Value}"
            End Select
        End If
    End Sub
    So the formatting is done based on the above logic or some other assertions. Just keep it simple as when attempts are made to go with complex assertions this can cause flickering on the DataGridView.


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, October 4, 2019 3:53 PM
    Moderator
  • Hello,

    Here is pattern that does what you want, if there are additional thoughts like can I also change values outside of the assertion, it depends but with the code below this does solve your current question.

    Public Class Form1
        Private operations As New Operations
        Private bsOrderDetails As New BindingSource
        Private SpecialIndicator As String = " *"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            bsOrderDetails.DataSource = operations.GetOrderDetails()
            DataGridView1.DataSource = bsOrderDetails
        End Sub
        Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) _
            Handles DataGridView1.CellFormatting
    
            If DataGridView1.Columns(e.ColumnIndex).Name.Equals("UnitPrice") Then
                If CDbl(e.Value) > 14 Then
                    e.Value = $"{SpecialIndicator}{e.Value}"
                End If
            End If
        End Sub
        Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) _
            Handles DataGridView1.EditingControlShowing
    
            If DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("UnitPrice").Index Then
                CType(e.Control, TextBox).Text = CType(e.Control, TextBox).Text.Replace(SpecialIndicator, "")
                AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
            End If
    
        End Sub
        Private Sub TextBox_keyPress(sender As Object, e As KeyPressEventArgs)
            If Char.IsDigit(CChar(e.KeyChar)) = False Then
                e.Handled = True
            End If
        End Sub
        Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) _
            Handles DataGridView1.DataError
    
            MessageBox.Show(e.Exception.Message)
            e.Cancel = True
    
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thanks Karen, It works but i would like to know how do i call the CellFormatting event for every row ? because i change the prefix for every row based on a condition.

    Logic is based not on index rows but by an assertion such as below while the first post I gave did one assertion where you could have done one or more else if's.

    Here I use a select case

    Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) _
        Handles DataGridView1.CellFormatting
    
        If DataGridView1.Columns(e.ColumnIndex).Name.Equals("UnitPrice") Then
            Dim currentUnitPrice = CDbl(e.Value)
            Select Case currentUnitPrice
                Case 1 To 14
                    e.Value = $"+{e.Value}"
                Case 14.25D To 20
                    e.Value = $"*{e.Value}"
                Case > 20
                    e.Value = $"~{e.Value}"
            End Select
        End If
    End Sub
    So the formatting is done based on the above logic or some other assertions. Just keep it simple as when attempts are made to go with complex assertions this can cause flickering on the DataGridView.


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    is there a way to do like a function because i cant write cases for all  ..i will have more than 100 cases. 
    Friday, October 4, 2019 8:05 PM
  • Hi,

    Can you describe in detail the function you want?

    Best Regards,

    Julie


    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.


    Thursday, October 31, 2019 8:08 AM
    Moderator