How to change values of column of DataTable and show only in DataGrid not updating actual table of database

大兔子大兔子 提交于 2019-12-25 17:14:13

问题


I have a column with encrypted name(all other columns are not encrypted) in SQL Database table. And I have to decrypt the column with encrypted name to show in DataGrid to users of my application but the actual table of SQL database should not be changed.(has to remain as encrypted name).

I think UpdateCommand works to update the actual table and I have to find an alternative than below UpdateCommand.

Or is there alternative way to decrypt only 1 column on DataTable which is not influencing the actual table of database?

My simple code is,

SqlCommand gridcomm = new SqlCommand();
gridcomm.Connection = Conn;

gridcomm.CommandText = "SELECT Id, customername, phonenumber FROM customers";

SqlDataAdapter gridda = new SqlDataAdapter(gridcomm);

SqlDataReader gridreader = gridcomm.ExecuteReader();
while (gridreader.Read())
{
}
gridreader.Close();

DataTable griddt = new DataTable("customers");
gridda.Fill(griddt);

foreach (DataRow row in griddt.Rows)
{
    string strcustomername = (string) row["customername"].ToString();
    bytecustomername = Convert.FromBase64String(strcustomername);
    string decryptedcustomername = DecryptStringFromBytes_Aes(bytecustomername, byteAESKey, byteAESIV);

    row["customername"] = decryptedcustomername;
}

gridda.UpdateCommand = new SqlCommandBuilder(gridda).GetUpdateCommand();

dataGrid_Totalcustomerlist.ItemsSource = griddt.DefaultView;
gridda.Update(griddt);

回答1:


Hello Kay Lee: I think that if you look at implementing a Coverter in your View you will get exactly what you are looking for. In your IValueConverter implementation you can Implement the Decrypt routine. A Converter is the extended syntax in a WPF Binding Statement. If this is not clear then I will flesh out some more. Here is a great reference for Converters: http://www.wpf-tutorial.com/data-binding/value-conversion-with-ivalueconverter/

Kind Regards, Mark Wardell




回答2:


I've read many posts but there were no solution for me as this case is unusual. However, I just thought logically and finally found solution by myself.

We just need to delete 2 line of Update related code because we don't need to update.

gridda.UpdateCommand = new SqlCommandBuilder(gridda).GetUpdateCommand();

gridda.Update(griddt);

Hope this helps someone..



来源:https://stackoverflow.com/questions/35835567/how-to-change-values-of-column-of-datatable-and-show-only-in-datagrid-not-updati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!