DBGrid control=set maxlength.

Is there a way to set the maxlength of a field in a data grid contol?
RUSTYAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
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
0
 
abhinavmCommented:
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.
0
 
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
RUSTYAuthor Commented:
Adjusted points to 20
0
 
trkcorpCommented:
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...
0
 
TheAnswerManCommented:
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
0
 
RUSTYAuthor Commented:
Ah.  Perfect.  Danke.  Aloha.  Tanx.  "A+"
0
All Courses

From novice to tech pro — start learning today.