How to prevent a user from using Quotations Marks?

aman_VB

Member
Joined
Apr 27, 2010
Messages
23
Programming Experience
1-3
Hi all,

I am using quotation marks are Delimiters. How do you prevent the user from ever using quotation marks in DataGridView cells?

I tried something like this but it never worked.

If DataGridView1.Columns.Contains(ControlChars.Quote) Then
MessageBox.Show("Man, that is my Delimiter")
Exit Sub
End If

If DataGridView1.Rows.Contains(ControlChars.Quote) Then
MessageBox.Show(errorMessage1)
Exit Sub
End If

Thanks in advance.
aman
 
example using DataGridView.CellValidating Event (System.Windows.Forms)
VB.NET:
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    Const delimiter As Char = """"c
    If e.FormattedValue.ToString.Contains(delimiter) Then
        e.Cancel = True
        Me.DataGridView1.Rows(e.RowIndex).ErrorText = delimiter & " character is reserved as delimiter."
    Else
        Me.DataGridView1.Rows(e.RowIndex).ErrorText = String.Empty
    End If
End Sub
I would also use a different delimiter than quote char...
 
Thanks John & Jim, but what would be a good Delimiter that is used by developers?

It depends on the data and how you want to store it. Commas and Tabs are two of the most common, but you might also use a pipe (|) as it is something that rarely occurs, if ever, in most data.
 
Back
Top