This is a migrated thread and some comments may be shown as answers.

Setting MaxLength based on database column

3 Answers 258 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 15 Aug 2014, 08:40 PM
We are using RadGrid with Entity Data Sources.  I'd like to be able to set the MaxLength of the text boxes in edit mode based on the size of the field in the database.  If a database column is varchar(10), then I'd like to set the MaxLength = 10.  Is there an easy way to do this?  I've got some ideas about going to the data source and pulling the column information but I'm wondering if that information is already somewhere in the grid object since it was bound to the entity object.

Any ideas / thoughts would be appreciated.

3 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 20 Aug 2014, 12:58 PM
Hello Steven,

In order to dynamically set the MaxLength property you can subscribe to the OnItemDataBound event of the grid, obtain a reference to the editor and set a value of your preferences. On how to reference the editor according to the edit type you can review this help article. Additionally note that the edit item holds the entity object that can be accessed as demonstrated below.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        GridEditableItem editItem = e.Item as GridEditableItem;
        if (editItem != null && editItem.IsInEditMode)
        {
            object dataItem = editItem.DataItem;
            //cast the dataItem to the respective Entity object type
        }
    }

If the above information does not help you resolve the matter please share with us the markup and code-behind of the page so we could examine the scenario and provide a more precise answer.

Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Steven
Top achievements
Rank 1
answered on 21 Aug 2014, 07:38 PM
I was actually wondering if the grid itself knows the datatype of the column from the binding.  Say the database column is called Part and has a datatype of varchar(20) and the grid is bound to an entitydatasource, I'm wondering if the grid has that information available.  If so, I could then do a set MAXLENGTH from the code behind based on the data type of varchar(20) and make it 20 characters long.  Otherwise I'll have to manually set each column to the MAXLENGTH manually - something I'm hoping to avoid doing.

Since a grid can auto create columns and know if it is a date / boolean / string field based on the datatype, if there is a way of getting to that information.

Thanks,
Steve
0
Angel Petrov
Telerik team
answered on 26 Aug 2014, 01:21 PM
Hi Steve,

Actually the grid knows the data type of the column but it is mapped to a .NET type. For example if you have a varchar(20) and varchar(40) columns they both will be mapped to System.String. Moreover the grid extracts the types in such cases from the entity object which uses the above mapping. Considering this it seems that extracting the actual database field length is not possible.

As an alternative solution I recommend constructing a custom mechanism which to return a certain max length value according to the field name(since you know the length of the field). That is probably not the best solution but it would at least allow you to dynamically set the MaxLength property.

Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Steven
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Steven
Top achievements
Rank 1
Share this question
or