Link to home
Start Free TrialLog in
Avatar of RUSTY
RUSTY

asked on

DBGrid control=set maxlength.

Is there a way to set the maxlength of a field in a data grid contol?
Avatar of abhinavm
abhinavm

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.
Avatar of RUSTY

ASKER

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.
Avatar of RUSTY

ASKER

Adjusted points to 20
Rusty,
  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...
ASKER CERTIFIED SOLUTION
Avatar of TheAnswerMan
TheAnswerMan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of RUSTY

ASKER

Ah.  Perfect.  Danke.  Aloha.  Tanx.  "A+"