Nyx Assasin
Nyx Assasin

Reputation: 141

How to make Datagridview Editable and Change it to Number Format?

Good Morning.

I have a Datagridview in a Form and it is connected in Database and it looks like this.

enter image description here

I have no problem with this part.

My question here is like this. How can I make the Fourth Column Editable? I mean I can edit it by clicking this property

enter image description here

and now the output will be like this.

enter image description here

Now here is the real question, I will ask a question based on the flow that my system will do.

1.The 4th Column are the column that will become editable and the rest will be locked or uneditable

2.Lets say I will put 48 how can I make it 48.00 when i leave the cell? That kind of format with .00 at the end.

3.Unable to input Letters on the 4th column.

TYSM for future help

Upvotes: 0

Views: 7657

Answers (3)

user15457418
user15457418

Reputation: 1

Private Sub dgvwithdraw_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvwithdraw.CellClick
    Select Case dgvwithdraw.Columns(e.ColumnIndex).Name
        Case "Select", "Alloted"
            dgvwithdraw.ReadOnly = False
        Case Else
            dgvwithdraw.ReadOnly = True
    End Select
End Sub

Upvotes: -1

jmcilhinney
jmcilhinney

Reputation: 54417

  1. Set the ReadOnly property to True for any column that you don't want the user to be able to edit.
  2. Set the DefaultCellStyle.Format property of the column to "n2" or "f2".
  3. I'd probably advise against that because the fact that you want to include a decimal point makes it more complex. If you're determined to go ahead then you should research how to allow only numeric input in a regular TextBox, because this will work exactly the same way. You simply need to access the TextBox control used for editing via the appropriate event handlers of the grid.

In the example below, because the editingTextBox field is declared WithEvents, the last method will handle the TextChanged event for the editing control while it's assigned to that field for the duration of the editing session.

Private WithEvents editingTextBox As TextBox

Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    Me.editingTextBox = DirectCast(e.Control, TextBox)
End Sub

Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    Me.editingTextBox = Nothing
End Sub

Private Sub editingTextBox_TextChanged(sender As Object, e As EventArgs) Handles editingTextBox.TextChanged
    '...
End Sub

Upvotes: 2

F0r3v3r-A-N00b
F0r3v3r-A-N00b

Reputation: 3003

Set the other columns to readonly = true and your numeric column = false and set the defaultcellstyle.format of your numeric column to "###,##0.00" and in your datagridview's cellvalidating event, do the ff:

Private Sub DatagridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DatagridView1.CellValidating
    Try
        If DatagridView1.IsCurrentCellDirty Then
            Select Case DatagridView1.Columns(e.ColumnIndex).Name.ToUpper
                Case "<NAME OF YOUR NUMERIC COLUMN>"
                    If Not IsNumeric(e.FormattedValue) Then
                        MsgBox("Invalid value.")
                        e.Cancel = True
                        Exit Sub
                    End If

                    If CType(e.FormattedValue, Integer) < 0 Then
                        MsgBox("Invalid value.")
                        e.Cancel = True
                        Exit Sub
                    End If
            End Select
        End If
    Catch ex As Exception
        ErrMsg(ex)
    End Try
End Sub

Upvotes: 1

Related Questions