简体   繁体   English

如何将 DataGridView 格式化为密码字符?

[英]How to format DataGridView to password characters?

Goal:目标:

My goal is to format 1 column to password characters * .我的目标是将 1 列格式化为密码字符*

Resources I have tried:我尝试过的资源:

I have used an example from here :我从这里使用了一个例子:

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.cellformatting?view=netframework-4.8 https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.cellformatting?view=netframework-4.8

and tried this answer here:并在这里尝试了这个答案:

https://stackoverflow.com/a/38074239/12485722 https://stackoverflow.com/a/38074239/12485722

This is my current code to load DataGrid View:这是我当前加载 DataGrid 视图的代码:

private void Form3_Load(object sender, EventArgs e)
{
    RefreshGrid();
}



//Refresh Data Grid from external Forms
public void RefreshGrid()
{
    // MySQL connection string
    using (var conn = new MySqlConnection(ConnectionString()))
    {
        using (var mySqlDataAdapter = new MySqlDataAdapter("select * from user", conn))
        {
            using (var dataSet = new DataSet())
            {
                DataSet DS = new DataSet();
                mySqlDataAdapter.Fill(DS);
                dataGridView1.DataSource = DS.Tables[0];
            }
        }
    }
}

This simply loads up the DataGridView with Usernames and passwords.这只是加载带有用户名和密码的 DataGridView。

Now I need to format 2nd column to password characters, so the passwords do not show as text but formatted as * .现在我需要将第二列格式化为密码字符,因此密码不显示为文本而是格式化为*

I have tried adding this piece of code from the 2nd resource mentioned above:我尝试从上面提到的第二个资源中添加这段代码:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 1 && e.Value != null)
    {
        e.Value = new String('*', e.Value.ToString().Length);
    }
}

This does not work - no errors show up, but I have noticed this references is set to 0.这不起作用 - 没有出现错误,但我注意到此引用设置为 0。

(Note: this is what it should be) (注意:这是应该的)

图片

Question:题:

I am confused where it is going wrong.. can somebody assist me to achieve this task?我很困惑哪里出错了..有人可以帮助我完成这项任务吗?

Update (1) - Events and cell formatting not showing:更新 (1) - 事件和单元格格式未显示:

在此处输入图片说明

在此处输入图片说明

Your method is not wired to an event of GridView (thats why it has 0 references, because nothing is using your function).您的方法没有连接到 GridView 的事件(这就是为什么它有 0 个引用,因为没有任何东西在使用您的函数)。

The solution in the mentioned link is correct, but you should wire your method to an event in designer (right click on grid view > properties > events) and choose an event to fire up in CellFormatting event.提到的链接中的解决方案是正确的,但您应该将您的方法连接到设计器中的事件(右键单击网格视图 > 属性 > 事件)并选择要在 CellFormatting 事件中触发的事件。 Also dont forget to handle EditingControlShowing, but everything what you need is already written in a solution thread that you mentioned.也不要忘记处理 EditingControlShowing,但是您需要的一切都已经写在您提到的解决方案线程中。

你为什么不修改你的查询,只说以下内容:

select username, repeat('*', length(password)) from users

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM