Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Custom DataGridViewColumn property serialization problem
 

Custom DataGridViewColumn property serialization problem

Hi
I have created custom DataGridView column and cell. They are working fine but if I add a new property to the column class for example and try to give it a value in the form designer, the value is not serialized to the code. (consider a string property) If I set a value for the property in the constructor of the class that value will be serialized to the code but if I try to change it in form designer new value will never serialized to the code.

even in the sample provided by microsoft (which is a maskedit column ) you can see this problem.

any idea or sample code how to fix this problem?
Mehdi58  Monday, July 27, 2009 7:07 AM

Hi Mehdi58,

We need to override the Clone method of DataGridViewColumn to support copying values of the custom properties. This is the code snippet:

public class MyDataGridViewColumn : DataGridViewColumn
{
    //The custom property.
    private bool _showValue;
    [DefaultValue(false)]
    public bool ShowValue
    {
        get { return _showValue; }
        set { _showValue = value; }
    }
    //Override this method to set the custom properties.
    public override object Clone()
    {
        MyDataGridViewColumn col = base.Clone() as MyDataGridViewColumn;
        col.ShowValue = ShowValue;
        return col;
    }
}


Let me know if this helps.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byMehdi58 Sunday, August 02, 2009 7:15 AM
  •  
Aland Li  Tuesday, July 28, 2009 12:37 PM

Hi Mehdi58,

We need to override the Clone method of DataGridViewColumn to support copying values of the custom properties. This is the code snippet:

public class MyDataGridViewColumn : DataGridViewColumn
{
    //The custom property.
    private bool _showValue;
    [DefaultValue(false)]
    public bool ShowValue
    {
        get { return _showValue; }
        set { _showValue = value; }
    }
    //Override this method to set the custom properties.
    public override object Clone()
    {
        MyDataGridViewColumn col = base.Clone() as MyDataGridViewColumn;
        col.ShowValue = ShowValue;
        return col;
    }
}


Let me know if this helps.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byMehdi58 Sunday, August 02, 2009 7:15 AM
  •  
Aland Li  Tuesday, July 28, 2009 12:37 PM
Thats amazing Thank you it works.
Mehdi58  Sunday, August 02, 2009 7:15 AM

You can use google to search for other answers

Custom Search

More Threads

• thread make hang dataGridView
• Binding ArrayList (of Structures) to a Datagrid Question?
• DataGridViewImageColumn - dynamically set images per grid row
• DataAdapter problem
• AutoComplete Textbox in a datagridview control not working
• How do I create a copy of a datatable - and the copy not have any reference to original?
• Change BindingNavigator.Validate behaviour
• Sorting a DataGridView Programatically
• Please select to the Combobox in the DataGridview
• Combobox + drop down menu + alt+tab = DBNull error