Hi Ryan,
I have written a test sample regarding your scenario, however, I found that
once the DataColumn type in the datasource is of type "Integer", the
DataGridView will format the data into "#,###" without any problem. The
full sample code is listed below:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable("test")
dt.Columns.Add(New DataColumn("column1", GetType(Integer)))
dt.Columns.Add(New DataColumn("column2", GetType(String)))
Dim i As Integer
For i = 0 To 5
Dim dr As DataRow
dr = dt.NewRow()
dr("column1") = i * 1000
dr("column2") = "item" + i.ToString()
dt.Rows.Add(dr)
Next
Me.Column1.DataPropertyName = "column1"
Me.Column1.ValueType = GetType(Integer)
Me.Column1.DefaultCellStyle.Format = "#,###"
Me.Column2.DataPropertyName = "column2"
Me.Column1.ValueType = GetType(String)
Me.DataGridView1.DataSource = dt
End Sub
Note: Column1 and Column2 are 2 DataGridViewTextBoxColumn(s) added from
designer.
Based on my test, once the "column1" is of type "Integer", the manually
input integer data in "column1" will be formatted into "#,###" style
without any problem. So, do you use "Integer" type in the datacolumn?
To restrict the user input as numeric data, you have 2 approaches:
#1, Restrict single key input to decimal character. You may get this done
by register the KeyDown event of TextBox in the DataGridViewTextBoxColumn
and filter non-decimal keys with SuppressKeyPress property, the sample code
is listed below:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.DataGridViewEditingControlSho wingEventArgs) Handles
DataGridView1.EditingControlShowing
If Me.DataGridView1.CurrentCell.ColumnIndex = 0 And Not e.Control
Is Nothing Then
Dim tb As TextBox = CType(e.Control, TextBox)
AddHandler tb.KeyDown, AddressOf TextBox_KeyDown
End If
End Sub
Private Sub TextBox_KeyDown(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyEventArgs)
If Keys.D0 < e.KeyData And e.KeyData < Keys.D9 Then
e.Handled = False
Else
e.Handled = True
e.SuppressKeyPress = True
End If
End Sub
The sample code handles DataGridView.EditingControlShowing event and get
the TextBox reference through e.Control property. Then it registers the
event for TextBox.
#2, Do not validate the single key press, but delay the validation to the
commit time. You may handle DataGridView.CellValidating event and validate
the UI input. If the input is incorrect(based on the Integer.TryPause
result), you may set Cancel property to true to cancel the commit:
Private Sub DataGridView1_CellValidating(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewCellValidatingEve ntArgs)
Handles DataGridView1.CellValidating
If e.ColumnIndex = 0 And Not e.FormattedValue = String.Empty Then
Dim i As Integer
Dim str As String = CType(e.FormattedValue, String)
str = str.Replace(",", String.Empty)
If Not Integer.TryParse(str, i) Then
MsgBox("Please input integer data")
e.Cancel = True
End If
End If
End Sub
These 2 code snippets both work well on my side. You may choose one of
these 2 solutions based on your application's requirement.
Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.