pedroPG
pedroPG

Reputation: 123

C# Custom property value lost when build

I have a problem with a custom class. What happens is that it loses its value after the project is compiled. Can someone help me?

I have the following custom class:

public class DataGridViewTextBoxColumnCustom : System.Windows.Forms.DataGridViewTextBoxColumn
{
   public bool EstaDisponibleParaFiltro { get; set; }
}

The problem is that in the form designer, when I add a column of type DataGridViewTextBoxCOlumnCustom to the DataGridView control and set its property ThisAvailableParaFilter to true, the value always returns to false. I hope you can give me a hand with this.

EDIT 1:

This is my custom class:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Solucion.Presentacion.controles
{
    public class DataGridViewTextBoxColumnCustom : System.Windows.Forms.DataGridViewTextBoxColumn
    {
        public bool EstaDisponibleParaFiltro { get; set; }
    }
}

In the DataGridView control I add one ColumnType, DataGridViewTextBoxColumnCustom, and set EstaDisponibleParaFiltro property to true:

enter image description here

But when I click Ok, the property always returns to false. I need to fix it.

Upvotes: 1

Views: 95

Answers (1)

pedroPG
pedroPG

Reputation: 123

Ok, i was able to fix the problem as follows:

  1. Adding the following attributes to the property:
[Browsable(true)]
[DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool EstaDisponibleParaFiltro { get; set; }
  1. Overriding the clone method:
public override object Clone()
{
   var clone = (DataGridViewTextBoxColumnCustom)base.Clone();
   clone.EstaDisponibleParaFiltro = this.EstaDisponibleParaFiltro;
   return clone;
}
  1. That's it, thank you all for your answers.

Upvotes: 1

Related Questions