Is there an easy way to set max input length for grid columns

Hi,

Im looking fir a nice clean and easy way to set the maximum input length of a grid columns please.

I am not interested in using the "ValidationRules" for this purpose - I simply want to prevent any more than X amount of characters entered.

I also saw an example of using an event to capture data input, and manually preventing data entry - but adding that to every column seems like a lot of work and maintenance.

I have used the html attributes elsewhere and I am ok with this approach, but couldnt get the following to work

  <GridColumn Field=@nameof(ContactDetailModel.Phone)
                Width="150" MinWidth="150" MaxWidth="150"
                EditType="EditType.DefaultEdit" EditorSettings="@PhoneEditParams">
    </GridColumn>

private IEditorSettings PhoneEditParams = new StringEditCellParams
  {
    Params = new TextBoxModel()
    {
      HtmlAttributes = new Dictionary<string, object>() { { "maxlength", DTOLibrary.DbFieldLengths.ContactDetails.Phone } }
    }
  };

any suggestions would be much appreciated,

Thanks,
Jeremy




3 Replies

RN Rahul Narayanasamy Syncfusion Team December 8, 2020 01:29 PM UTC

Hi Jeremy, 

Greetings from Syncfusion. 

Query: Is there an easy way to set max input length for grid columns 

We have validated your query and you want to set maxlength property using HtmlAttributes. We suggest you to achieve your requirement by rendering a SfTextBox and set HtmlAttributes using EditTemplate. Find the below code snippets and sample for your reference. 

 
<SfGrid DataSource="@OrderData" Toolbar=@ToolbarItems> 
    <GridEditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Center" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer ID" EditType="EditType.DefaultEdit" TextAlign="TextAlign.Center" Width="130"> 
            <EditTemplate> 
                <SfTextBox ID="CustomerID" HtmlAttributes="@(new Dictionary<string, object>() { { "maxlength", 10 } })" @bind-Value="@((context as Order).CustomerID)"></SfTextBox> 
            </EditTemplate> 
        </GridColumn> 
        . . . 
    </GridColumns> 
</SfGrid> 




Please let us know if you have any concerns. 

Regards, 
Rahul 
 



JE Jeremy December 9, 2020 09:43 AM UTC

Brilliant, thanks for that, I have applied to code to my software and works as expected


RN Rahul Narayanasamy Syncfusion Team December 10, 2020 04:37 AM UTC

Hi Jeremy, 
 
Thanks for the update. 
 
We are happy to hear that the provided solution was helpful to achieve your requirement. 
 
Please get back to us if you need further assistance. 
 
Regards, 
Rahul 


Loader.
Up arrow icon