Click here to Skip to main content
15,905,008 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,

i did the code for password columns in the datagridview but it is not working.
My code:
C#
private void dgvCompanyUsers_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
       {


           if (dgvCompanyUsers.CurrentCell.OwningColumn.HeaderText == "Password")
           {
               DataGridViewTextBoxEditingControl textBox = e.Control as DataGridViewTextBoxEditingControl;
               if (textBox != null)
               {
                  // textBox.KeyPress -= new KeyPressEventHandler(Control_KeyPress);
                   textBox.KeyPress += new KeyPressEventHandler(Control_KeyPress);
               }
           }

   public void Control_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
       {
           //if (e.KeyChar >= '0' && e.KeyChar <= '9')
           //{
           //    e.Handled = true;
           //}
           TextBox s = ((TextBox)sender);
           if (s.ToString() != "")
           {
              // e.Handled = true;
               e.KeyChar = s.PasswordChar;


           }

how to set the (*) in the password column in the datagridview.how to write code in the keypress event for setting (*).

[Edit]Code block added[/Edit]
Posted
Updated 29-Mar-13 4:04am
v2
Comments
ZurdoDev 29-Mar-13 10:11am    
I would think in your item template you could use a textbox and set its mode to password. Have you tried that?
sadhana4 29-Mar-13 10:19am    
where i can find the item template in the datagridview?

1 solution

I am not sure about availability of ItemTemplate in WinForms DataGridView.
Also, I am not sure about KeyPress Event being the best Event for this purpose. nevertheless,

To convert content of your text box (named 's' in your example) into a string of 'PasswordChar', you can use the following code:

.
if (s.Text.Length > 0)
{
    string strToShow="";
    foreach(char ch in s.Text.ToCharArray())
    {
        strToShow+="*";
    }
}
s.Text=strToShow;


And I hope you had stored the original value of keyStroke somewhere so that you can have original password rather than this 'PasswordString'. Also, before applying the above code, make sure you have handled BackSpace/Delete/Enter/UnwantedCharactersFromPasswordCriteria .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900