Re: Type convertsion from string



Andrus <kobruleht2@xxxxxx> wrote:
Using VCS 2005 Express .NET 2 WinForms.

I implemented entity databinding in DataGridView OnCellValuePushed method as

PropertyInfo p = entityTmp.GetType().GetProperty(
Columns[e.ColumnIndex].DataPropertyName);
// e.Value type is string
p.SetValue(entityTmp, e.Value, null);

This works only for string property types.
For other types SetValue() causes System.ArgumentException like

Object of type 'System.String' cannot be converted to type 'System.Decimal'.

How to change this code so that SetValue() works for non-string types ?
I need to convert string type (e.Value) to property type:

How to convert string to property p type ?

Have you tried Convert.ChangeType?

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.