Is it possible to make a cell behave like Password Control

8 posts, 0 answers
  1. Hari M S
    Hari M S avatar
    1 posts
    Member since:
    Apr 2010

    Posted 06 Apr 2010 Link to this post

    Hi

           Is it possible to make a cell in GridView Control behave like Password Control? I mean, We can change the Edit Text Box to Password Field, so that the user can't view the things he typed in, same thing acn we implment in Grid Cell?..

    Waiting for a Positive Reply as well as Thanking you in advance..

    Thanks
    Hari
  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 08 Apr 2010 Link to this post

    Hi Hari M S,

    You can achieve this behavior. You should obfuscate the text that is displayed in the cells when they are not in edit mode. Hence, you need to subscribe for the CellFormatting  event. You can use the following code snippet as sample:

    private void radGridView_CellFormatting(object sender, CellFormattingEventArgs e)
     {
         GridViewDataColumn dataColumn = e.CellElement.ColumnInfo as GridViewDataColumn;
      
         if (dataColumn != null && dataColumn.UniqueName == "Password")
         {
             object value = e.CellElement.RowInfo.Cells["Password"].Value;
             string text = String.Empty;
             if (value != null)
             {
                 int passwordLen = Convert.ToString(value).Length;
                 text = String.Join("*", new string[passwordLen]);
             }
      
             e.CellElement.Text = text;
         }
     }

    Also you need to make a text box editor to be a password box. You can do it by handling CellEditorInitialized event.

    private void radGridView_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        GridViewDataColumn dataColumn = e.Column as GridViewDataColumn;
      
        if (dataColumn != null && dataColumn.UniqueName == "Password")
        {
            RadTextBoxEditor textBoxEditor = this.radGridView.ActiveEditor as RadTextBoxEditor;
              
            if (textBoxEditor != null)
            {
                RadTextBoxEditorElement editorElement = textBoxEditor.EditorElement as RadTextBoxEditorElement;
                editorElement.PasswordChar = '*';
            }
        }
    }

    If you have further questions feel free to write us back.

    Greetings,
    Svett
    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. Jenson
    Jenson avatar
    25 posts
    Member since:
    Oct 2012

    Posted 07 Oct 2012 Link to this post

    I follow the codes closely, and managed to display the text in the password column as a series of ' * '.
    However, I noticed that, once this is triggered, the column behind all following the password column to display the user input value as a series of  ' * ' too, including those alrady have data inside the cells.

    Thank you.

    Regards,
    Jenson
  4. Svett
    Admin
    Svett avatar
    728 posts

    Posted 10 Oct 2012 Link to this post

    Hello Jenson,

    This happens due to the editor's caching and RadGridView's UI virtualization. You should improve the code snippets in the following way:
    private void radGridView_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        GridViewDataColumn dataColumn = e.Column as GridViewDataColumn;
     
        if (dataColumn != null)
        {
            RadTextBoxEditor textBoxEditor = this.radGridView1.ActiveEditor as RadTextBoxEditor;
     
            if (textBoxEditor != null)
            {
                RadTextBoxEditorElement editorElement = textBoxEditor.EditorElement as RadTextBoxEditorElement;
     
                if (dataColumn.UniqueName == "Password")
                {
                    editorElement.PasswordChar = '*';
                }
                else
                {
                    editorElement.PasswordChar = '\0';
                }
            }
        }
    }

    If the proposed solution does not address the misleading behavior, I would kindly ask you open support ticket where you can enclose a sample project where the issue occurs. In addition, could you illustrate the exact steps that we should follow to reproduce it. Greetings,
    Svett
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  5. Jenson
    Jenson avatar
    25 posts
    Member since:
    Oct 2012

    Posted 11 Oct 2012 Link to this post

    Thanks Svett,

    Thanks for the helps, it works!

    Cheers,
    Jenson
  6. Kuba
    Kuba avatar
    13 posts
    Member since:
    Aug 2013

    Posted 16 Dec 2013 Link to this post

    Hi!

    This solution is almost great. ;) I found one issue, when you can see password.

    When you write password in new line it is obfuscated, but when you move to the next cell it became visible until you end editing new line.
    It applies only to new row.

    How can I avoid this problem? Is this my fault or code from above works this way?

    Cheers,
    Kuba.
  7. George
    Admin
    George avatar
    500 posts

    Posted 19 Dec 2013 Link to this post

    Hi Kuba,

    Thank you for writing.

    You need to subscribe to the CellFormatting event of RadGridView and use the following code (which my colleague provided):
    GridViewDataColumn dataColumn = e.CellElement.ColumnInfo as GridViewDataColumn;
     
    if (dataColumn != null && dataColumn.UniqueName == "Password")
    {
        object value = e.CellElement.RowInfo.Cells["Password"].Value;
        string text = String.Empty;
        if (value != null)
        {
            int passwordLen = Convert.ToString(value).Length;
            text = String.Join("*", new string[passwordLen]);
        }
     
        e.CellElement.Text = text;
    }

    You can also find attached below a sample project which demonstrates this functionality.

    I hope this helps.

    Regards,
    George
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  8. Rayan
    Rayan avatar
    3 posts
    Member since:
    Jan 2014

    Posted 25 Oct 2017 in reply to Svett Link to this post

    Perfect! Thank you Svett
Back to Top