RUSTY
asked on
DBGrid control=set maxlength.
Is there a way to set the maxlength of a field in a data grid contol?
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.
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.
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).DataWi dth = 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...
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).DataWi
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
Ah. Perfect. Danke. Aloha. Tanx. "A+"