DataGridView trying to cast from string to object
I have a custom control whose Value
property is an object. The control also has a Text
property which, depending on the object, displays an object's string property.
This custom control is hosted within a DataGridView
and I implemented the required interface IDataGridViewEditingControl
to get it working. I also have 2 classes inheriting from DataGridViewColumn
and DataGridViewTextBoxCell
.
The CustomerTypeDto class:
public class CustomerTypeDto
{
public int Id {get; set;}
public int Description {get; set}
//Other properties...
}
One remaining problem is that after I select a value from the control and the DataGridView tries to end the cell edit, I get the following exception:
System.FormatException: Invalid cast from 'System.String' to 'CustomerTypeDto'. ---> System.InvalidCastException: Invalid cast from 'System.String' to 'CustomerTypeDto'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)
--- End of inner exception stack trace ---
at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)
at System.Windows.Forms.Formatter.ParseObjectInternal(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue)
at System.Windows.Forms.Formatter.ParseObject(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)
at System.Windows.Forms.DataGridViewCell.ParseFormattedValueInternal(Type valueType, Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
at System.Windows.Forms.DataGridViewCell.ParseFormattedValue(Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
at System.Windows.Forms.DataGridView.PushFormattedValue(DataGridViewCell& dataGridViewCurrentCell, Object formattedValue, Exception& exception)
Which method or property do I need to override so that the DataGridView
can cast from my object to string and vice-versa.
And should I inherit from DataGridViewTextBoxCell
or from DataGridViewCell
directly?
EDIT
This is my cell class:
public class CustomerTypeCell : DataGridViewTextBoxCell
{
public CustomerTypeCell()
: base()
{ }
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
CustomControl ctl = DataGridView.EditingControl as CustomControl;
if (this.Value == null)
ctl.Value = (CustomerTypeDto)this.DefaultNewRowValue;
else
ctl.Value = (CustomerTypeDto)this.Value;
}
public override Type EditType
{
get { return typeof(CustomControl); }
}
public override Type ValueType
{
get { return typeof(CustomerTypeDto); }
}
public override object DefaultNewRowValue
{
get { return null; }
}
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
}
}
The property FormattedValueType
in DataGridViewTextBoxCell
always returns string.
And in the method ParseFormattedValue
above, it's trying to cast from string to my object. The description of this method :
Converts a value formatted for display to an actual cell value.
But this doesn't make sense because the Value
is of type CustomerTypeDto
, so how is this parsing going to work?
Basically what I'm trying to do is let the user select a CustomerType
object from my custom control. This object should be the cell's value, and the value's text (in this case the Description
property) is displayed as a string in the cell.
I do not understand why the DataGridView
wants to parse the string in an object if I already have the object in the cell's value property.
c# visual-studio visual-studio-2012
|
show 1 more comment
I have a custom control whose Value
property is an object. The control also has a Text
property which, depending on the object, displays an object's string property.
This custom control is hosted within a DataGridView
and I implemented the required interface IDataGridViewEditingControl
to get it working. I also have 2 classes inheriting from DataGridViewColumn
and DataGridViewTextBoxCell
.
The CustomerTypeDto class:
public class CustomerTypeDto
{
public int Id {get; set;}
public int Description {get; set}
//Other properties...
}
One remaining problem is that after I select a value from the control and the DataGridView tries to end the cell edit, I get the following exception:
System.FormatException: Invalid cast from 'System.String' to 'CustomerTypeDto'. ---> System.InvalidCastException: Invalid cast from 'System.String' to 'CustomerTypeDto'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)
--- End of inner exception stack trace ---
at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)
at System.Windows.Forms.Formatter.ParseObjectInternal(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue)
at System.Windows.Forms.Formatter.ParseObject(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)
at System.Windows.Forms.DataGridViewCell.ParseFormattedValueInternal(Type valueType, Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
at System.Windows.Forms.DataGridViewCell.ParseFormattedValue(Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
at System.Windows.Forms.DataGridView.PushFormattedValue(DataGridViewCell& dataGridViewCurrentCell, Object formattedValue, Exception& exception)
Which method or property do I need to override so that the DataGridView
can cast from my object to string and vice-versa.
And should I inherit from DataGridViewTextBoxCell
or from DataGridViewCell
directly?
EDIT
This is my cell class:
public class CustomerTypeCell : DataGridViewTextBoxCell
{
public CustomerTypeCell()
: base()
{ }
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
CustomControl ctl = DataGridView.EditingControl as CustomControl;
if (this.Value == null)
ctl.Value = (CustomerTypeDto)this.DefaultNewRowValue;
else
ctl.Value = (CustomerTypeDto)this.Value;
}
public override Type EditType
{
get { return typeof(CustomControl); }
}
public override Type ValueType
{
get { return typeof(CustomerTypeDto); }
}
public override object DefaultNewRowValue
{
get { return null; }
}
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
}
}
The property FormattedValueType
in DataGridViewTextBoxCell
always returns string.
And in the method ParseFormattedValue
above, it's trying to cast from string to my object. The description of this method :
Converts a value formatted for display to an actual cell value.
But this doesn't make sense because the Value
is of type CustomerTypeDto
, so how is this parsing going to work?
Basically what I'm trying to do is let the user select a CustomerType
object from my custom control. This object should be the cell's value, and the value's text (in this case the Description
property) is displayed as a string in the cell.
I do not understand why the DataGridView
wants to parse the string in an object if I already have the object in the cell's value property.
c# visual-studio visual-studio-2012
Can you add an example of the text in the cell and the definition of the CustomerTypeDto class?
– Steve Mitcham
Mar 30 '15 at 15:39
@Ivan-Mark Debono t looks likes PwnyExpress was already on the right track. I found a similar question here on stack overflow at: stackoverflow.com/questions/1407689/…
– orgtigger
Mar 30 '15 at 16:06
I deleted my answer after I re-read your comment. I'm unclear as to why a search filter that is user entered requires an ID. Is there a way you can change this to just taking astring
?
– Steve Mitcham
Apr 1 '15 at 13:06
The ID is needed so that the a valid CustomerType object can be used in the Customer.CustomerType property. The Customer object is then sent back to the server (WebApi 2) and the graph is saved back in the db. I have a sample project here... onedrive.live.com/redir?resid=ba8db0c447edb711%211334
– Ivan-Mark Debono
Apr 1 '15 at 13:14
How exactly do you do the databinding? (thus how do you bind the list of CustomerTypeDto's to the view / to the columns/fields). In the worst case it could be that the problem happens when he tries to update/add the data in the datasource but that would be if you made an error with the binding
– Thomas
Apr 3 '15 at 14:02
|
show 1 more comment
I have a custom control whose Value
property is an object. The control also has a Text
property which, depending on the object, displays an object's string property.
This custom control is hosted within a DataGridView
and I implemented the required interface IDataGridViewEditingControl
to get it working. I also have 2 classes inheriting from DataGridViewColumn
and DataGridViewTextBoxCell
.
The CustomerTypeDto class:
public class CustomerTypeDto
{
public int Id {get; set;}
public int Description {get; set}
//Other properties...
}
One remaining problem is that after I select a value from the control and the DataGridView tries to end the cell edit, I get the following exception:
System.FormatException: Invalid cast from 'System.String' to 'CustomerTypeDto'. ---> System.InvalidCastException: Invalid cast from 'System.String' to 'CustomerTypeDto'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)
--- End of inner exception stack trace ---
at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)
at System.Windows.Forms.Formatter.ParseObjectInternal(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue)
at System.Windows.Forms.Formatter.ParseObject(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)
at System.Windows.Forms.DataGridViewCell.ParseFormattedValueInternal(Type valueType, Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
at System.Windows.Forms.DataGridViewCell.ParseFormattedValue(Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
at System.Windows.Forms.DataGridView.PushFormattedValue(DataGridViewCell& dataGridViewCurrentCell, Object formattedValue, Exception& exception)
Which method or property do I need to override so that the DataGridView
can cast from my object to string and vice-versa.
And should I inherit from DataGridViewTextBoxCell
or from DataGridViewCell
directly?
EDIT
This is my cell class:
public class CustomerTypeCell : DataGridViewTextBoxCell
{
public CustomerTypeCell()
: base()
{ }
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
CustomControl ctl = DataGridView.EditingControl as CustomControl;
if (this.Value == null)
ctl.Value = (CustomerTypeDto)this.DefaultNewRowValue;
else
ctl.Value = (CustomerTypeDto)this.Value;
}
public override Type EditType
{
get { return typeof(CustomControl); }
}
public override Type ValueType
{
get { return typeof(CustomerTypeDto); }
}
public override object DefaultNewRowValue
{
get { return null; }
}
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
}
}
The property FormattedValueType
in DataGridViewTextBoxCell
always returns string.
And in the method ParseFormattedValue
above, it's trying to cast from string to my object. The description of this method :
Converts a value formatted for display to an actual cell value.
But this doesn't make sense because the Value
is of type CustomerTypeDto
, so how is this parsing going to work?
Basically what I'm trying to do is let the user select a CustomerType
object from my custom control. This object should be the cell's value, and the value's text (in this case the Description
property) is displayed as a string in the cell.
I do not understand why the DataGridView
wants to parse the string in an object if I already have the object in the cell's value property.
c# visual-studio visual-studio-2012
I have a custom control whose Value
property is an object. The control also has a Text
property which, depending on the object, displays an object's string property.
This custom control is hosted within a DataGridView
and I implemented the required interface IDataGridViewEditingControl
to get it working. I also have 2 classes inheriting from DataGridViewColumn
and DataGridViewTextBoxCell
.
The CustomerTypeDto class:
public class CustomerTypeDto
{
public int Id {get; set;}
public int Description {get; set}
//Other properties...
}
One remaining problem is that after I select a value from the control and the DataGridView tries to end the cell edit, I get the following exception:
System.FormatException: Invalid cast from 'System.String' to 'CustomerTypeDto'. ---> System.InvalidCastException: Invalid cast from 'System.String' to 'CustomerTypeDto'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)
--- End of inner exception stack trace ---
at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)
at System.Windows.Forms.Formatter.ParseObjectInternal(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue)
at System.Windows.Forms.Formatter.ParseObject(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)
at System.Windows.Forms.DataGridViewCell.ParseFormattedValueInternal(Type valueType, Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
at System.Windows.Forms.DataGridViewCell.ParseFormattedValue(Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
at System.Windows.Forms.DataGridView.PushFormattedValue(DataGridViewCell& dataGridViewCurrentCell, Object formattedValue, Exception& exception)
Which method or property do I need to override so that the DataGridView
can cast from my object to string and vice-versa.
And should I inherit from DataGridViewTextBoxCell
or from DataGridViewCell
directly?
EDIT
This is my cell class:
public class CustomerTypeCell : DataGridViewTextBoxCell
{
public CustomerTypeCell()
: base()
{ }
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
CustomControl ctl = DataGridView.EditingControl as CustomControl;
if (this.Value == null)
ctl.Value = (CustomerTypeDto)this.DefaultNewRowValue;
else
ctl.Value = (CustomerTypeDto)this.Value;
}
public override Type EditType
{
get { return typeof(CustomControl); }
}
public override Type ValueType
{
get { return typeof(CustomerTypeDto); }
}
public override object DefaultNewRowValue
{
get { return null; }
}
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
}
}
The property FormattedValueType
in DataGridViewTextBoxCell
always returns string.
And in the method ParseFormattedValue
above, it's trying to cast from string to my object. The description of this method :
Converts a value formatted for display to an actual cell value.
But this doesn't make sense because the Value
is of type CustomerTypeDto
, so how is this parsing going to work?
Basically what I'm trying to do is let the user select a CustomerType
object from my custom control. This object should be the cell's value, and the value's text (in this case the Description
property) is displayed as a string in the cell.
I do not understand why the DataGridView
wants to parse the string in an object if I already have the object in the cell's value property.
c# visual-studio visual-studio-2012
c# visual-studio visual-studio-2012
edited Mar 31 '15 at 7:03
Ivan-Mark Debono
asked Mar 27 '15 at 11:52
Ivan-Mark DebonoIvan-Mark Debono
4,6921457132
4,6921457132
Can you add an example of the text in the cell and the definition of the CustomerTypeDto class?
– Steve Mitcham
Mar 30 '15 at 15:39
@Ivan-Mark Debono t looks likes PwnyExpress was already on the right track. I found a similar question here on stack overflow at: stackoverflow.com/questions/1407689/…
– orgtigger
Mar 30 '15 at 16:06
I deleted my answer after I re-read your comment. I'm unclear as to why a search filter that is user entered requires an ID. Is there a way you can change this to just taking astring
?
– Steve Mitcham
Apr 1 '15 at 13:06
The ID is needed so that the a valid CustomerType object can be used in the Customer.CustomerType property. The Customer object is then sent back to the server (WebApi 2) and the graph is saved back in the db. I have a sample project here... onedrive.live.com/redir?resid=ba8db0c447edb711%211334
– Ivan-Mark Debono
Apr 1 '15 at 13:14
How exactly do you do the databinding? (thus how do you bind the list of CustomerTypeDto's to the view / to the columns/fields). In the worst case it could be that the problem happens when he tries to update/add the data in the datasource but that would be if you made an error with the binding
– Thomas
Apr 3 '15 at 14:02
|
show 1 more comment
Can you add an example of the text in the cell and the definition of the CustomerTypeDto class?
– Steve Mitcham
Mar 30 '15 at 15:39
@Ivan-Mark Debono t looks likes PwnyExpress was already on the right track. I found a similar question here on stack overflow at: stackoverflow.com/questions/1407689/…
– orgtigger
Mar 30 '15 at 16:06
I deleted my answer after I re-read your comment. I'm unclear as to why a search filter that is user entered requires an ID. Is there a way you can change this to just taking astring
?
– Steve Mitcham
Apr 1 '15 at 13:06
The ID is needed so that the a valid CustomerType object can be used in the Customer.CustomerType property. The Customer object is then sent back to the server (WebApi 2) and the graph is saved back in the db. I have a sample project here... onedrive.live.com/redir?resid=ba8db0c447edb711%211334
– Ivan-Mark Debono
Apr 1 '15 at 13:14
How exactly do you do the databinding? (thus how do you bind the list of CustomerTypeDto's to the view / to the columns/fields). In the worst case it could be that the problem happens when he tries to update/add the data in the datasource but that would be if you made an error with the binding
– Thomas
Apr 3 '15 at 14:02
Can you add an example of the text in the cell and the definition of the CustomerTypeDto class?
– Steve Mitcham
Mar 30 '15 at 15:39
Can you add an example of the text in the cell and the definition of the CustomerTypeDto class?
– Steve Mitcham
Mar 30 '15 at 15:39
@Ivan-Mark Debono t looks likes PwnyExpress was already on the right track. I found a similar question here on stack overflow at: stackoverflow.com/questions/1407689/…
– orgtigger
Mar 30 '15 at 16:06
@Ivan-Mark Debono t looks likes PwnyExpress was already on the right track. I found a similar question here on stack overflow at: stackoverflow.com/questions/1407689/…
– orgtigger
Mar 30 '15 at 16:06
I deleted my answer after I re-read your comment. I'm unclear as to why a search filter that is user entered requires an ID. Is there a way you can change this to just taking a
string
?– Steve Mitcham
Apr 1 '15 at 13:06
I deleted my answer after I re-read your comment. I'm unclear as to why a search filter that is user entered requires an ID. Is there a way you can change this to just taking a
string
?– Steve Mitcham
Apr 1 '15 at 13:06
The ID is needed so that the a valid CustomerType object can be used in the Customer.CustomerType property. The Customer object is then sent back to the server (WebApi 2) and the graph is saved back in the db. I have a sample project here... onedrive.live.com/redir?resid=ba8db0c447edb711%211334
– Ivan-Mark Debono
Apr 1 '15 at 13:14
The ID is needed so that the a valid CustomerType object can be used in the Customer.CustomerType property. The Customer object is then sent back to the server (WebApi 2) and the graph is saved back in the db. I have a sample project here... onedrive.live.com/redir?resid=ba8db0c447edb711%211334
– Ivan-Mark Debono
Apr 1 '15 at 13:14
How exactly do you do the databinding? (thus how do you bind the list of CustomerTypeDto's to the view / to the columns/fields). In the worst case it could be that the problem happens when he tries to update/add the data in the datasource but that would be if you made an error with the binding
– Thomas
Apr 3 '15 at 14:02
How exactly do you do the databinding? (thus how do you bind the list of CustomerTypeDto's to the view / to the columns/fields). In the worst case it could be that the problem happens when he tries to update/add the data in the datasource but that would be if you made an error with the binding
– Thomas
Apr 3 '15 at 14:02
|
show 1 more comment
2 Answers
2
active
oldest
votes
Your CustomerTypeDto
class needs an explicit cast operator for the string type.
class CustomerTypeDto
{
// string -> CustomerTypeDto
public static explicit operator CustomerTypeDto(string s)
{
CustomerTypeDto ctd = new CustomerTypeDto();
// ... do something with the string.
return ctd;
}
// CustomerTypeDto -> string
public static explicit operator String(CustomerTypeDto ctd)
{
return ctd.toString();
// or some other way to return it's string value.
}
// other stuff...
}
So you can do things like this:
return (CustomerTypeDto)someString;
Unfortunately this answer doesn't work for me because I keep getting the same error. Why does theDataGridView
want to parse the string in an object anyway?
– Ivan-Mark Debono
Mar 31 '15 at 7:09
add a comment |
if you want to implement your value ahead of time,
use method e.ParsingApplied = true;
after you implement to e.Value = (ObjOfYourType)
i have used it in event CellParsing
that is caused before dropping an exception.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29300070%2fdatagridview-trying-to-cast-from-string-to-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your CustomerTypeDto
class needs an explicit cast operator for the string type.
class CustomerTypeDto
{
// string -> CustomerTypeDto
public static explicit operator CustomerTypeDto(string s)
{
CustomerTypeDto ctd = new CustomerTypeDto();
// ... do something with the string.
return ctd;
}
// CustomerTypeDto -> string
public static explicit operator String(CustomerTypeDto ctd)
{
return ctd.toString();
// or some other way to return it's string value.
}
// other stuff...
}
So you can do things like this:
return (CustomerTypeDto)someString;
Unfortunately this answer doesn't work for me because I keep getting the same error. Why does theDataGridView
want to parse the string in an object anyway?
– Ivan-Mark Debono
Mar 31 '15 at 7:09
add a comment |
Your CustomerTypeDto
class needs an explicit cast operator for the string type.
class CustomerTypeDto
{
// string -> CustomerTypeDto
public static explicit operator CustomerTypeDto(string s)
{
CustomerTypeDto ctd = new CustomerTypeDto();
// ... do something with the string.
return ctd;
}
// CustomerTypeDto -> string
public static explicit operator String(CustomerTypeDto ctd)
{
return ctd.toString();
// or some other way to return it's string value.
}
// other stuff...
}
So you can do things like this:
return (CustomerTypeDto)someString;
Unfortunately this answer doesn't work for me because I keep getting the same error. Why does theDataGridView
want to parse the string in an object anyway?
– Ivan-Mark Debono
Mar 31 '15 at 7:09
add a comment |
Your CustomerTypeDto
class needs an explicit cast operator for the string type.
class CustomerTypeDto
{
// string -> CustomerTypeDto
public static explicit operator CustomerTypeDto(string s)
{
CustomerTypeDto ctd = new CustomerTypeDto();
// ... do something with the string.
return ctd;
}
// CustomerTypeDto -> string
public static explicit operator String(CustomerTypeDto ctd)
{
return ctd.toString();
// or some other way to return it's string value.
}
// other stuff...
}
So you can do things like this:
return (CustomerTypeDto)someString;
Your CustomerTypeDto
class needs an explicit cast operator for the string type.
class CustomerTypeDto
{
// string -> CustomerTypeDto
public static explicit operator CustomerTypeDto(string s)
{
CustomerTypeDto ctd = new CustomerTypeDto();
// ... do something with the string.
return ctd;
}
// CustomerTypeDto -> string
public static explicit operator String(CustomerTypeDto ctd)
{
return ctd.toString();
// or some other way to return it's string value.
}
// other stuff...
}
So you can do things like this:
return (CustomerTypeDto)someString;
edited Mar 30 '15 at 15:40
answered Mar 30 '15 at 15:33
pwnyexpresspwnyexpress
848514
848514
Unfortunately this answer doesn't work for me because I keep getting the same error. Why does theDataGridView
want to parse the string in an object anyway?
– Ivan-Mark Debono
Mar 31 '15 at 7:09
add a comment |
Unfortunately this answer doesn't work for me because I keep getting the same error. Why does theDataGridView
want to parse the string in an object anyway?
– Ivan-Mark Debono
Mar 31 '15 at 7:09
Unfortunately this answer doesn't work for me because I keep getting the same error. Why does the
DataGridView
want to parse the string in an object anyway?– Ivan-Mark Debono
Mar 31 '15 at 7:09
Unfortunately this answer doesn't work for me because I keep getting the same error. Why does the
DataGridView
want to parse the string in an object anyway?– Ivan-Mark Debono
Mar 31 '15 at 7:09
add a comment |
if you want to implement your value ahead of time,
use method e.ParsingApplied = true;
after you implement to e.Value = (ObjOfYourType)
i have used it in event CellParsing
that is caused before dropping an exception.
add a comment |
if you want to implement your value ahead of time,
use method e.ParsingApplied = true;
after you implement to e.Value = (ObjOfYourType)
i have used it in event CellParsing
that is caused before dropping an exception.
add a comment |
if you want to implement your value ahead of time,
use method e.ParsingApplied = true;
after you implement to e.Value = (ObjOfYourType)
i have used it in event CellParsing
that is caused before dropping an exception.
if you want to implement your value ahead of time,
use method e.ParsingApplied = true;
after you implement to e.Value = (ObjOfYourType)
i have used it in event CellParsing
that is caused before dropping an exception.
answered Nov 23 '18 at 14:07
Sergey RogSergey Rog
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29300070%2fdatagridview-trying-to-cast-from-string-to-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you add an example of the text in the cell and the definition of the CustomerTypeDto class?
– Steve Mitcham
Mar 30 '15 at 15:39
@Ivan-Mark Debono t looks likes PwnyExpress was already on the right track. I found a similar question here on stack overflow at: stackoverflow.com/questions/1407689/…
– orgtigger
Mar 30 '15 at 16:06
I deleted my answer after I re-read your comment. I'm unclear as to why a search filter that is user entered requires an ID. Is there a way you can change this to just taking a
string
?– Steve Mitcham
Apr 1 '15 at 13:06
The ID is needed so that the a valid CustomerType object can be used in the Customer.CustomerType property. The Customer object is then sent back to the server (WebApi 2) and the graph is saved back in the db. I have a sample project here... onedrive.live.com/redir?resid=ba8db0c447edb711%211334
– Ivan-Mark Debono
Apr 1 '15 at 13:14
How exactly do you do the databinding? (thus how do you bind the list of CustomerTypeDto's to the view / to the columns/fields). In the worst case it could be that the problem happens when he tries to update/add the data in the datasource but that would be if you made an error with the binding
– Thomas
Apr 3 '15 at 14:02