Doug Robinson Doug Robinson - 3 years ago 248 Question

modify (encrypt/decrypt) cell values in rows of datagridview

I've followed this article to use a datagridview to manage data that will be saved into an XML file:

The data will be a list of usernames and passwords.

As such, I need to step through each cell in the columns for 'username' and 'password', and replace the value of the cell with the result of a call to a function that would encrypt or decrypt the value of the cell.

On form_load, after I populate the table with data from the XML file, I want to cycle through these cells and do this to decrypt, and on form close / save, I want to cycle through each cell and encrypt the values before written to file.

I have a function written out to encrypt the data, the part I'm stuck on is how I could step through every cell in the 'username' and 'password' columns of DataGridView1 (as an example) and update the results to the value returned by a function.

I apologize for not having a code example for this question, I do not know how to do this, so I haven't been able to put together a bit of code to try / fail at it.

I imagine it will be something simple like 'For each cell in (whatever statement returns the cells in a given column of the datagridview), ...' , but I'm not sure.

Answer Source

Create a Dataset and use Dataset.ReadXml Method to read the Xml Data And then choose the specified datatable from the dataset as a datasource for the datagridView. it is easier to manipulate datatable rows.

use the decryption function on the DatagridView.formatingRow event

and before closing Loop over Datatable.rows

For Each row As DataRow In dtDataTable.Rows
row("Pass") = Encrypt(row("Pass")

and encrypt the password before saving it. and Save the dataset into the XML File using the Method WriteXml

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download