DBGrid control=set maxlength.

Is there a way to set the maxlength of a field in a data grid contol?
Who is Participating?
TheAnswerManConnect With a Mentor Commented:
I assume you dont have TrueGrid Pro5..
That IS a pretty badass Grid Control, Its got some cool unbound grid features too.

'Flop this Code in
Private Sub Grid1_KeyPress(KeyAscii As Integer)
 if len(Grid1) > 10 then Keyascii = 0
End Sub

'this will keep you from entering more than 10 characters per field
No there is no direct way to set maxlength of a field. but you can do it by writing some code in keypress event or beforeupdate event, which will check the length of text and if it is more than maximum length you can prompt to user or whatever you want.
RUSTYAuthor Commented:
I've been trying to write some code to do this, but cannot get it to read the len of the field in the data grid column.  Can you post a snippet of code that will do this.  Thanks.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

RUSTYAuthor Commented:
Adjusted points to 20
  True DBGrid Pro 5.0 by APEX (the upgrade to DBGrid) allows you to do this very simply.  It has a DataWidth property that you can set on a Column object.  
Ex: TDBGrid1.Columns(1).DataWidth = 255
I have used it ALOT. It prevents users from keying in data that is too long to fit in the field which as I'm sure you're aware causes an error.  I know this is probably not what you want to hear but I thought you might like to know... True DBGrid really is worth buying...
you could try this too.. if you know what size for each column is

Private Sub Grid1_KeyPress(KeyAscii As Integer)
 Select Case Grid1.col
 case 0
  if len(Grid1) > 1 then Keyascii = 0
 case 1
  if len(Grid1) > 2 then Keyascii = 0
 case 2
  if len(Grid1) > 3 then Keyascii = 0
 end select
End Sub

'this will allow one character in the first
2 in the second..
and three in the third
RUSTYAuthor Commented:
Ah.  Perfect.  Danke.  Aloha.  Tanx.  "A+"
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.