Display decrypted text in databound controls


G

Guest

Hi

I have a table in an sql database that contains data that has been encrypted
using the DPAPI.

What I am trying to achieve is to bind several controls on a vb 2005 windows
form to those fields but display the decrypted data in the controls instead
of the encrypted data. I also want to use a binding navigator to scroll
through the records and to add/delete records. I don't have a problem with
the encryption/decryption but am having trouble doing it dynamically with the
bound controls.

I have tried using several of the bindingsource events but they seem occur
when opening the form or at other times.

Any assistance would be appreciated.
Ian
 
Ad

Advertisements

J

Jeffrey Tan[MSFT]

Hi Ian,

This normally varies based on the databound controls. For simple
databinding, such as TextBox, you may use Binding.Parse/Format events to
customize the display in the TextBox. In Binding.Format event, you may
decrypt the underlying encrypted data and display the result clear text to
the UI. In Binding.Parse event, you should do the opposite: get the text in
the UI TextBox and encrypt it. The final encrypted data will be pushed into
the underlying datasource. The article below talks more details of the
Binding.Parse/Format events:
"Data binding concepts in .NET windows forms"
http://www.codeproject.com/vb/net/databindingconcepts.asp

For DataGridView complex databinding, it provides similar
DataGridView.CellParsing/CellFormatting events which can leverage with the
same logic above.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Ian,

Thanks for your feedback.

I originally provided some sample code regarding using Binding.Parse/Format
events in the link below, for your information:
http://groups.google.com/group/microsoft.public.dotnet.framework.windowsform
s.databinding/msg/a429f83e8a552123?hl=en&

Also, the article I provided in the last reply also contains sample code in
VB.net regarding how to use Binding.Parse/Format events:
"Data binding concepts in .NET windows forms"
http://www.codeproject.com/vb/net/databindingconcepts.asp

I can not find a dedicate article regarding the usage of
DataGridView.CellParsing/CellFormatting events. Basicly, you may select
DataGridView control in the VS2005 designer and click the event icon in the
PropertyBrowser and find CellParsing/CellFormatting events in the event
list. Then you may double click to add an event handler for these 2 events.

Below is the official link for DataGridView.CellParsing event, which
contains the most useful guidelines and sample code regarding its usage:
http://msdn2.microsoft.com/en-gb/library/system.windows.forms.datagridview.c
ellparsing.aspx

Anyway, if you still have any problem of getting this work for you, please
feel free to tell me, I will work with you, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

RobinS

Here's an example from a posting I made back in November 2006 for
capturing and using the Format and Parse events on a DataGridView.

The OP was trying to change how the data is displayed, but
not how it's stored, so if the user puts in "75000", he wanted
it displayed as "75K", but stored as 75000. And when it's
brought in from the database, he wanted to convert it from
75000 to 75K. When it's put back, he wanted to translate it
the other way.

CellFormatting happens between the data source and the screen.
CellParsing happens between the screen and the data source.

Try this:

AddHandler myGrid.CellFormatting, AddressOf OnCellFormatting
AddHandler myGrid.CellParsing, AddressOf OnCellParsing

Private Sub OnCellFormatting(ByVal object as Sender, _
ByVal e as DataGridViewCellFormattingEventArgs)

If e.ColumnIndex = myGrid.Columns("columnIcareAbout") Then
'setting this to true signals the grid that this
' column is being dynamically updated. If you don't
' do this, you will get an infinite loop if you have
' also set the column to have its size automatically
' determined.
e.FormattingApplied = True

'get the row being populated; format this cell
Dim row as DataGridViewRow = myGrid.Rows(e.RowIndex)
If e.Value = "75000" Then
e.Value = "75K"
End If
End If
End Sub

Private Sub OnCellParsing(ByVal object as Sender, _
ByVal e as DataGridViewCellParsingEventArgs)

If e.ColumnIndex = myGrid.Columns("columnIcareAbout") Then
'get the row being populated; format this cell
Dim row as DataGridViewRow = myGrid.Rows(e.RowIndex)
If e.Value = "75K" Then
e.Value = "75000"
End If
End If
End Sub

Hope this helps.
Robin S.
-------------------
"Jeffrey Tan[MSFT]" said:
Hi Ian,

Thanks for your feedback.

I originally provided some sample code regarding using
Binding.Parse/Format
events in the link below, for your information:
http://groups.google.com/group/microsoft.public.dotnet.framework.windowsform
s.databinding/msg/a429f83e8a552123?hl=en&

Also, the article I provided in the last reply also contains sample
code in
VB.net regarding how to use Binding.Parse/Format events:
"Data binding concepts in .NET windows forms"
http://www.codeproject.com/vb/net/databindingconcepts.asp

I can not find a dedicate article regarding the usage of
DataGridView.CellParsing/CellFormatting events. Basicly, you may
select
DataGridView control in the VS2005 designer and click the event icon
in the
PropertyBrowser and find CellParsing/CellFormatting events in the
event
list. Then you may double click to add an event handler for these 2
events.

Below is the official link for DataGridView.CellParsing event, which
contains the most useful guidelines and sample code regarding its
usage:
http://msdn2.microsoft.com/en-gb/library/system.windows.forms.datagridview.c
ellparsing.aspx

Anyway, if you still have any problem of getting this work for you,
please
feel free to tell me, I will work with you, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent
issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each
follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach
the
most efficient resolution. The offering is not appropriate for
situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are
best
handled working with a dedicated Microsoft Support Engineer by
contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
J

Jeffrey Tan[MSFT]

Hi Robin,

Thank you for sharing your code snippet!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ad

Advertisements

R

RobinS

You're welcome. In all fairness, I figured that out with the help
of Brian Noyes' Data Binding book. It has some really cool stuff in it.

Robin S.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top