[英]How to retrieve password from c# Winforms datagridview cell after masking while saving the data
I have used below codes to mask my password in datagridview and i am successfully doing it. 我已经使用下面的代码在datagridview中屏蔽了我的密码,并且我成功地做到了。
private void gvInstanceDetails_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 4)
{
if (e.Value != null)
{
gvInstanceDetails.Rows[e.RowIndex].Tag = e.Value;
e.Value = new string('\u2022', e.Value.ToString().Length);
}
}
}
private void gvInstanceDetails_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox t = e.Control as TextBox;
if (t != null)
{
t.UseSystemPasswordChar = true;
}
}
Once I mask, I am unable to retrieve the value that is getting entered by user into the password field, if user is not deleting the entire text and re enter the password. 屏蔽后,如果用户没有删除整个文本并重新输入密码,则无法检索用户在密码字段中输入的值。
eg: - if password is like ***** and user replaces last two characters and enter 34 then while saving i am getting string as ***34. 例如:-如果密码像*****,并且用户替换最后两个字符并输入34,则在保存的同时我将字符串获取为*** 34。
Thanks 谢谢
The EditingControl
uses FormattedValue
of the cell for editing. EditingControl
使用单元格的FormattedValue
进行编辑。
As a quick fix, in the EditingControlShowing
handler set the Value
of the CurrentCell
as Text
of the TextBox
: 作为快速解决方案,在
EditingControlShowing
处理程序中,将CurrentCell
的Value
设置为TextBox
Text
:
private void DataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 4)
{
var txt = (TextBox)e.Control;
txt.Text = $"{dataGridView1.CurrentCell.Value}";
txt.UseSystemPasswordChar = true;
}
}
As a better option, create a DataGridViewPasswordColumn
which handles the logic for you: 作为更好的选择,创建一个
DataGridViewPasswordColumn
来为您处理逻辑:
public class DataGridViewPasswordColumn : DataGridViewTextBoxColumn
{
public DataGridViewPasswordColumn()
{
this.CellTemplate = new DataGriViewPasswordCell();
}
}
public class DataGriViewPasswordCell : DataGridViewTextBoxCell
{
public override void InitializeEditingControl(int rowIndex,
object initialFormattedValue,
DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex,
initialFormattedValue, dataGridViewCellStyle);
((TextBox)this.DataGridView.EditingControl).UseSystemPasswordChar = true;
}
protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds,
int rowIndex, DataGridViewElementStates cellState,
object value, object formattedValue,
string errorText, DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
var i = $"{formattedValue}".Length;
formattedValue = new string('●', i);
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
cellState, value, formattedValue,
errorText, cellStyle, advancedBorderStyle, paintParts);
}
}
Not that i use winforms DataGrids. 并非我使用winforms DataGrids。 However...
然而...
You are killing the data with 您正在使用杀死数据
e.Value = new string('\u2022', e.Value.ToString().Length);
However you are putting into a tag 但是,您要放入标签
gvInstanceDetails.Rows[e.RowIndex].Tag = e.Value;
If you know the row you want, you just need to call 如果知道所需的行,只需致电
var myAwesomePassword = (string) gvInstanceDetails.Rows[someArbitaryRow].Tag
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.