Best way to set MaxLength on GridViewDataColumn

7 posts, 0 answers
  1. Geoff Hardy
    Geoff Hardy avatar
    58 posts
    Member since:
    Feb 2010

    Posted 28 Sep 2010 Link to this post

    The TextBox control has a useful property called MaxLength which limits how many characters you can enter in it.

    There does not seem to be an obvious/simple way to do the same thing with a GridViewDataColumn.

    The way I got this to work was:

    <Controls:GridViewDataColumn DataMemberBinding="{Binding ProductCode,Mode=TwoWay}" Header="Product Code" Width="150">
      <Controls:GridViewDataColumn.CellEditTemplate>
        <DataTemplate>
          <TextBox Text="{Binding ProductCode,Mode=TwoWay}" MaxLength="50"/>
        </DataTemplate>
       </Controls:GridViewDataColumn.CellEditTemplate>
    </Controls:GridViewDataColumn>

    Is there a better/simpler/less verbose way of achieving this?
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 28 Sep 2010 Link to this post

    Hi Geoff Hardy,

    This is a good way to achieve that  and there is no need to change it .

    As an alternative however , you may use the EditorStyle property of the GridViewDataColumn.
    You will need to set it to a style targeting TextBox, and having one setter - for the MaxLenght property.

    Regards,
    Pavel Pavlov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. John
    John avatar
    17 posts
    Member since:
    Aug 2014

    Posted 28 Jan in reply to Pavel Pavlov Link to this post

    When supplying the new style for EditorStyle, what must it be based on for implicit styling?The editor looks a bit different of the style is not based on anything.
  5. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    281 posts

    Posted 01 Feb Link to this post

    Hi John,

    Please try the following approach:
    <Style TargetType="TextBox" x:Key="myTxtStyle" BasedOn="{StaticResource TextBoxStyle}">
               <Setter Property="MaxLength" Value="10"/>
    </Style>
    I have tested it from my end and the MaxLenght property is applied correctly. The default view of the editor is kept as well.

    Please update me if there are any issues.

    Regards,
    Stefan Nenchev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. John
    John avatar
    17 posts
    Member since:
    Aug 2014

    Posted 03 Feb in reply to Stefan Nenchev Link to this post

    Thanks. That works.
  7. Rob Ainscough
    Rob Ainscough avatar
    172 posts
    Member since:
    Jan 2010

    Posted 23 Aug Link to this post

    So creating a new Style for every possible length restriction I may have is the solution?  Come on folks, that's not a solution.  Why didn't Telerik add MaxLength to GridViewDataColumn ... that's why pay for these controls.

    Rob

  8. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    281 posts

    Posted 24 Aug Link to this post

    Hi Rob,

    Having in mind that it is possible to apply an EditorStyle for the columns or edit the CellTemplate/CellEditTemplate, we have decided to not expose such property on a column level as the mentioned features provide the user with a much wider set of modifications to apply.  Furthermore, creating such style and assigning it to the different columns` EditorStyle should not take much effort as well. We will consider exposing such property if we notice a high demand from our customers.

    Regards,
    Stefan Nenchev
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
DevCraft banner