简体   繁体   English

数据网格视图会更改特定列的所有值

[英]Data Grid View alter all the values of a specific column

I am really new to Data grid view, so my question could seem pretty basic to most of you. 我对数据网格视图真的很陌生,因此对于大多数人来说,我的问题似乎很基本。 I have a database table with passwords. 我有一个带密码的数据库表。 These passwords are encrypted. 这些密码是加密的。 I have a C# app with a datagridview which shows me this table, but I see the encrypted passwords, and i would like to see them decrypted. 我有一个带有datagridview的C#应用​​程序,它向我显示了此表,但是我看到了加密的密码,我希望看到它们被解密。

This code populates my datagrid with the database table named Companies(each company has a password): 这段代码使用名为Companies(每个公司都有一个密码)的数据库表填充我的数据网格:

private void populateCompaniesDataGrid()
        {
            String sql = "SELECT * from companies";
            MySqlCommand command = new MySqlCommand(sql, dh.Connection);

            try
            {
                MySqlDataAdapter adapter = new MySqlDataAdapter();
                adapter.SelectCommand = command;
                DataTable dbdataset = new DataTable();
                adapter.Fill(dbdataset);
                BindingSource bSource = new BindingSource();

                bSource.DataSource = dbdataset;
                dataGridView1.DataSource = bSource;
                adapter.Update(dbdataset);

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                dh.Connection.Close();
            }
        }

I have a class DataEncryptor, which has 2 methods - Encrypt and Decrypt. 我有一个DataEncryptor类,它有2种方法-加密和解密。

private DataEncryptor encryptor = new DataEncryptor();
public string Encrypt(string text)
    {
       //does some stuff to encrypt
        return Encrypt;
    }

    public string Decypt(string text)
    {
        //does some stuff to decrypt
        return Decrypt;
    } 

You can add this and call your Decyrpt method. 您可以添加它并调用Decyrpt方法。

void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
            {
                if (e.ColumnIndex == 1 && e.RowIndex != this.dataGridView1.NewRowIndex)
                {
                    e.Value = Decrypt(e.Value.ToString());
                }
            }

Maybe try RowsAdded event? 也许尝试RowFyre事件? Then within the loop add your code to read the password, call you decrypt method and write the string back to the row. 然后在循环中添加您的代码以读取密码,调用您的解密方法并将字符串写回到行中。

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    for (int i = e.RowIndex; i < e.RowCount + e.RowIndex; i++)
    {
        //not tested: 
        string password = dataGridView1.Rows[e.RowIndex].Cells["encryptedPassword"].Value.ToString()
        string decryptedPassword = DataEncryptor.Decrypt(password);

        dataGridView1.Rows[e.RowIndex].Cells["encryptedPassword"].Value = decryptedPassword;
        Console.WriteLine("Row " + i.ToString() + " added");
    }
}

gridview rowdatabound event in winforms? 在WinForms中GridView RowDatabound事件?

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

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