FormatString in RadDropDownList

5 posts, 0 answers
  1. Anna
    Anna avatar
    7 posts
    Member since:
    Mar 2014

    Posted 10 Jul 2014 Link to this post

    Hi,
    I'm using build 2014.1.402.40 for WinForms. I need to format display of RadDropDownList items as phone number.
    This is the code:

    phoneDropDownList.FormattingEnabled = true;
    phoneDropDownList.FormatString = {0:###-###-####};

    This has no effect on strings populated in the list.
    What's the correct way to use this property?
    Just a note, I've tried other formats and they didn't work either, so I don't think it's a incorrect format string issue.
    Thank you.
  2. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 15 Jul 2014 Link to this post

    Hello Anna,

    Thank you for writing.

    '#' specifies digit placeholder. I am not sure how you populate the RadDropDownList with items, but formatting items with the FormatString property is applicable for bound mode and precisely for formatting the property specified in the DisplayMember. For unbound mode you can use the VisualListItemFormatting event.
    public Form1()
    {
        InitializeComponent();
        for (int i = 0; i < 5; i++)
        {
                        
            this.radDropDownList1.Items.Add("1112223333");
        }     
    }
     
    private void radDropDownList1_VisualListItemFormatting(object sender,
        VisualItemFormattingEventArgs args)
    {
        decimal value;
        if (decimal.TryParse(args.VisualItem.Text,out value))
        {
            args.VisualItem.Text=  string.Format("{0:###-###-####}",value);
        }
    }

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Anna
    Anna avatar
    7 posts
    Member since:
    Mar 2014

    Posted 16 Jul 2014 in reply to Dess Link to this post

    Hi,
    I think I'm starting to see where the problem is.
    The dropDown is bound to a list of objects:

    phoneDropDownList.DataSource = this.PhoneNumbers;  // Here PhoneNumbers is a List<ContactInfo>

    //ContactInfo class has Contact property that contains actual phone#.

     phoneDropDownList.DisplayMember = "Contact";
     phoneDropDownList.ValueMember = "Contact";
     phoneDropDownList.FormattingEnabled = true;
     phoneDropDownList.FormatString = "{0:###-###-####}";

    This probably doesn't work b/c it's trying to apply formatString to the object of type ContactInfo instead of Contact property?
    What is the right way to do it in this case?

    I've also tried  the event phoneDropDownList_VisualListItemFormatting(object sender, VisualItemFormattingEventArgs args)
    but args.VisualItem.Text contains "ContactInfo", not the actual phone number property, so formatting does not get applied here.
    In any case, how do I get to the actual PhoneNumber property to apply formatting to it?
    Thank you.
  5. Anna
    Anna avatar
    7 posts
    Member since:
    Mar 2014

    Posted 18 Jul 2014 in reply to Anna Link to this post

    Hi, sorry, the VisualListItemFormatting does work. But FormatString on DataBound list doesn't.
    Anyway, I can use the VisualListItemFormatting event. The issue now is that only items in dropDown are formatted, once I select an item, it displays as unformatted. Is there a way to format both, selected item and drop-down item?
    Also, any suggestions on why FormatString on DataBound item is not working?
    Thank you.
  6. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 21 Jul 2014 Link to this post

    Hello Anna,

    Thank you for writing back.

    Please find attached a sample project, demonstrating how to format the items in the pop up in bound mode via the FormatString property. In addition, not only the items in the list are formatted, but the text box in the editable part as well. I would like to note that the phone number should be stored in a property of numeric type in order to format its value correctly because the '#' symbol specifies digit placeholder. There is another RadDropDownList control in the sample project in unbound mode, which uses the VisualListItemFormatting event to format the text. In this case it is also necessary to format the text box in the editable part.

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Desislava
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WinForms is Visual Studio 2017 Ready