【问题标题】:How to retrieve password from c# Winforms datagridview cell after masking while saving the datac# - 如何在保存数据的同时屏蔽后从c#Winforms datagridview单元格中检索密码
【发布时间】:2018-05-23 05:54:37
【问题描述】:

我已使用以下代码在 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;
        }      
    } 

一旦我屏蔽,如果用户没有删除整个文本并重新输入密码,我将无法检索用户在密码字段中输入的值。

例如: - 如果密码类似于 ***** 并且用户替换最后两个字符并输入 34,那么在保存时我将字符串保存为 ***34。

谢谢

【问题讨论】:

  • 建议:不要将密码存储为纯文本!
  • 我建议您使用these dedicated classes 将此功能添加到您的网格中。我前段时间创建了它们,但是在刚刚测试它们时,我意识到它们遇到了您要修复的相同错误。我通过更改DataGridViewPasswordTextBoxCell.InitializeEditingControl 方法的第一行来解决这个问题。
  • 请注意,这些自定义列和单元格类可以简单地添加到您的项目中,然后您可以像添加任何其他列一样将密码列添加到网格中。然后,您只需将列的 UseSystemPasswordChar 属性设置为 True 即可。

标签: c# .net winforms datagridview


【解决方案1】:

快速修复

EditingControl 使用单元格的FormattedValue 进行编辑。

作为快速修复,在EditingControlShowing 处理程序中将CurrentCellValue 设置为TextBoxText

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;
    }
}

更好的选择 - 创建 DataGridViewPasswordColumn

作为更好的选择,创建一个 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);
    }
}

【讨论】:

    【解决方案2】:

    不是我使用 winforms DataGrids。不过……

    你正在用

    杀死数据
    e.Value = new string('\u2022', e.Value.ToString().Length);
    

    但是你要放入标签

     gvInstanceDetails.Rows[e.RowIndex].Tag = e.Value;
    

    如果你知道你想要的行,你只需要调用

    var myAwesomePassword = (string) gvInstanceDetails.Rows[someArbitaryRow].Tag
    

    【讨论】:

      猜你喜欢
      • 2010-12-17
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 2015-10-02
      • 2019-04-30
      • 1970-01-01
      相关资源
      最近更新 更多