none
cell null value RRS feed

  • Question

  • hi

    I do not want to leave an empty cell or a decimal point without numbers
    in datagridview

    code Numeric only

    Private Sub DATAGRID_BILL_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DATAGRID_BILL.EditingControlShowing
    
            Try
                If DATAGRID_BILL.CurrentCell.ColumnIndex = 3 Then
                    AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
    
                End If
            Catch ex As Exception
                Exit Sub
            End Try
          
        End Sub
    
        Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
            Dim numbers As Windows.Forms.TextBox = sender
            If InStr("1234567890.", e.KeyChar) = 0 And Asc(e.KeyChar) <> 8 Or (e.KeyChar = "." And InStr(numbers.Text, ".") > 0) Then
                e.KeyChar = Chr(0)
                e.Handled = True
            End If
        End Sub



    • Edited by ahmeddc Monday, January 14, 2019 3:18 PM
    Monday, January 14, 2019 3:18 PM

Answers

  • A different approach can be tried out here which I put together recently.

    https://1drv.ms/u/s!AtGAgKKpqdWjjGtZg7D8lyLntoZP

    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
    
            Dim departments = From Items In XDocument.Load("Departments.xml")...<Departments>
                              Select
                              Department = Items.<DepartmentName>.Value,
                              Number = Items.<DepartmentNumber>.Value,
                              Rating = CDec(Items.<Rating>.Value)
    
            For Each department In departments
                DataGridView1.Rows.Add(New Object() {department.Department, department.Number, department.Rating})
            Next
        End Sub
        Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) _
            Handles DataGridView1.CellFormatting
    
            If Not DataGridView1.Rows(e.RowIndex).IsNewRow Then
                If DataGridView1.Columns(e.ColumnIndex).Name.Equals("RatingColumn") Then
                    If e.Value Is Nothing Then
                        Exit Sub
                    End If
                    If Not e.Value.ToString.Contains(",") AndAlso Not e.Value.ToString.Contains(".") Then
                        e.Value = e.Value.ToString & ",00"
                    End If
                    If e.Value.ToString.Contains(".") Then
                        e.Value = e.Value.ToString.Replace(".", ",")
                    End If
                End If
            End If
        End Sub
    
        Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) _
            Handles DataGridView1.CellValidating
    
            Dim cell As DataGridViewCell = DataGridView1.Item(e.ColumnIndex, e.RowIndex)
    
            If cell.IsInEditMode Then
                Dim c As Control = DataGridView1.EditingControl
    
                If DataGridView1.Columns(e.ColumnIndex).Name = "RatingColumn" Then
                    Dim decValue As Decimal = 0
                    If Not Decimal.TryParse(c.Text, decValue) Then
                        MessageBox.Show("Please enter a valid decimal")
                        e.Cancel = True
                    Else
                        If Not c.Text.Contains(".") Then
                            MessageBox.Show("Must be a decimal value")
                            e.Cancel = True
                        End If
                    End If
                End If
            End If
        End Sub
    End Class
    


    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


    Monday, January 14, 2019 3:39 PM
    Moderator
  • Hi,

    If you don't want the cell to be empty, you can use the TextChange event .

     Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            DataGridView1.DefaultCellStyle.NullValue = "0"
            Try
                If DataGridView1.CurrentCell.ColumnIndex = 3 Then
                    AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_KeyPress
                    AddHandler CType(e.Control, TextBox).TextChanged, AddressOf TextBox_TextChanged
                End If
            Catch ex As Exception
                Exit Sub
            End Try
        End Sub
    
        Private Sub TextBox_KeyPress(sender As Object, e As KeyPressEventArgs)
            Dim numbers As Windows.Forms.TextBox = sender
            If (InStr("1234567890.", e.KeyChar) = 0 And Asc(e.KeyChar) <> 8) Or (e.KeyChar = "." And InStr(numbers.Text, ".") > 0) Then
                e.KeyChar = Chr(0)
                e.Handled = True
            End If
    
        End Sub
    
        Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)
            Dim numbers As Windows.Forms.TextBox = sender
            If numbers.Text = "" Then
                numbers.Text = "0"
            End If
        End Sub

    Best Regards,

    Alex


    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.

    • Marked as answer by ahmeddc Tuesday, January 15, 2019 5:50 PM
    Tuesday, January 15, 2019 6:18 AM

All replies

  • A different approach can be tried out here which I put together recently.

    https://1drv.ms/u/s!AtGAgKKpqdWjjGtZg7D8lyLntoZP

    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
    
            Dim departments = From Items In XDocument.Load("Departments.xml")...<Departments>
                              Select
                              Department = Items.<DepartmentName>.Value,
                              Number = Items.<DepartmentNumber>.Value,
                              Rating = CDec(Items.<Rating>.Value)
    
            For Each department In departments
                DataGridView1.Rows.Add(New Object() {department.Department, department.Number, department.Rating})
            Next
        End Sub
        Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) _
            Handles DataGridView1.CellFormatting
    
            If Not DataGridView1.Rows(e.RowIndex).IsNewRow Then
                If DataGridView1.Columns(e.ColumnIndex).Name.Equals("RatingColumn") Then
                    If e.Value Is Nothing Then
                        Exit Sub
                    End If
                    If Not e.Value.ToString.Contains(",") AndAlso Not e.Value.ToString.Contains(".") Then
                        e.Value = e.Value.ToString & ",00"
                    End If
                    If e.Value.ToString.Contains(".") Then
                        e.Value = e.Value.ToString.Replace(".", ",")
                    End If
                End If
            End If
        End Sub
    
        Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) _
            Handles DataGridView1.CellValidating
    
            Dim cell As DataGridViewCell = DataGridView1.Item(e.ColumnIndex, e.RowIndex)
    
            If cell.IsInEditMode Then
                Dim c As Control = DataGridView1.EditingControl
    
                If DataGridView1.Columns(e.ColumnIndex).Name = "RatingColumn" Then
                    Dim decValue As Decimal = 0
                    If Not Decimal.TryParse(c.Text, decValue) Then
                        MessageBox.Show("Please enter a valid decimal")
                        e.Cancel = True
                    Else
                        If Not c.Text.Contains(".") Then
                            MessageBox.Show("Must be a decimal value")
                            e.Cancel = True
                        End If
                    End If
                End If
            End If
        End Sub
    End Class
    


    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


    Monday, January 14, 2019 3:39 PM
    Moderator
  • Hi,

    If you don't want the cell to be empty, you can use the TextChange event .

     Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            DataGridView1.DefaultCellStyle.NullValue = "0"
            Try
                If DataGridView1.CurrentCell.ColumnIndex = 3 Then
                    AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_KeyPress
                    AddHandler CType(e.Control, TextBox).TextChanged, AddressOf TextBox_TextChanged
                End If
            Catch ex As Exception
                Exit Sub
            End Try
        End Sub
    
        Private Sub TextBox_KeyPress(sender As Object, e As KeyPressEventArgs)
            Dim numbers As Windows.Forms.TextBox = sender
            If (InStr("1234567890.", e.KeyChar) = 0 And Asc(e.KeyChar) <> 8) Or (e.KeyChar = "." And InStr(numbers.Text, ".") > 0) Then
                e.KeyChar = Chr(0)
                e.Handled = True
            End If
    
        End Sub
    
        Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)
            Dim numbers As Windows.Forms.TextBox = sender
            If numbers.Text = "" Then
                numbers.Text = "0"
            End If
        End Sub

    Best Regards,

    Alex


    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.

    • Marked as answer by ahmeddc Tuesday, January 15, 2019 5:50 PM
    Tuesday, January 15, 2019 6:18 AM