Bob Gatto

Bob Gatto

  • NA
  • 44
  • 8k

My password unmasking is working but not the way I want it to

Dec 1 2020 1:21 PM
I've masked a password column in a datagridview with this code:
  1. private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)  
  2. {  
  3.  if(e.ColumnIndex == 2 && e.Value != null){if (MouseButtons == MouseButtons.Left &&  
  4.   e.ColumnIndex==2 && e.RowIndex == dataGridView2.CurrentCell.RowIndex)  
  5.   {  
  6.  int idno = (int)dataGridView2.Rows[e.RowIndex].Cells["Id"].Value;  
  7.    SqlConnection c = new SqlConnection(ConStr);  
  8.    string query = "select * from Passwords where Id = " +  
  9.      idno;DataTable Tbl = new DataTable();  
  10.    using (SqlDataAdapter da = new SqlDataAdapter(query, c))  
  11.    {  
  12.      c.Open();  
  13.      da.Fill(Tbl);  
  14.      c.Close();  
  15.    }  
  16.    c.Dispose();  
  17.   dataGridView2.Rows[e.RowIndex].Cells[2].Value = Tbl.Rows[0]["password"].ToString();  
  18.  }  
  19.  else  
  20.  e.Value = new String('*', e.Value.ToString().Length);  
  21.  }  
  22. }  
I want to hold down the left mouse button and have only that cell that the cursor is over be unmasked and then have it masked again as soon as I release the mouse button. I only want this happening when the cursor is over the password column (which is columnindex 2).
 
The problem I'm having is the password column unmasks even if the mouse is not in the password column.
 
How can I fix this?
 
Thanks for your help,
Bob Gatto

Answers (2)