0

I Have a class that contains a string which is use to store multiple value (think in term of xml description) and an override of [] to put value in the string and getting it.

  class Entity
{
    private String _Data;
    public String Data
    {
        get { return (String)_Data; }
        set { _Data=value;}
    }

    public Object this[String propertyname]
    {
        get
        {
            return GetPropertyValue(propertyname);
        }
        set
        {
            SetPropertyValue(propertyname,Value);       
        }
    }

    private String GetPropertyValue(String propname)
    {
        if (Data == null) return "";
        String fbalise = "<" + propname + ">";
        int indexstart = Data.IndexOf(fbalise);
        if (indexstart == -1)
            //throw new Exception("Cannot Get Value because balise for " + propname + " does not exist");
            return null;
        indexstart += fbalise.Length;
        String ebalise = "</" + propname + ">";
        int caraclength = Data.IndexOf(ebalise, indexstart) - indexstart;
        return Data.Substring(indexstart, caraclength);
    }

   private void SetPropertyValue(String propname, String valuetoinsert)
    {
        if (Data == null) Data = "";
        String fbalise = "<" + propname + ">";
        String ebalise = "</" + propname + ">";
        int indexstart = Data.IndexOf(fbalise);
        if (indexstart == -1)
        {
            indexstart = Data.Length;
            Data = Data + fbalise + ebalise;
        }

        indexstart += fbalise.Length;

        int caraclength = Data.IndexOf(ebalise, indexstart) - indexstart;
        String lastvalue = Data.Substring(indexstart, caraclength);
        if (lastvalue != valuetoinsert)
        {
            Data = Data.Remove(indexstart, caraclength);
            Data = Data.Insert(indexstart, valuetoinsert);
        }
    }
}

Now I have a List<Entity> that i want to bind to a gridview by setting the datasource of the gridview.

after the binding it s only show the Data as column with all my text. (it s normal, i m ok with that, it s the normal rule) But Is it possible to specified that my binding, instead of research property, use the [] to get data and bind it to appropriate column (for example if i have a column "Name", call entity["Name"] instead of entity.Name which is not existing.....

  • You might take a look at Mark Gravell's answer to this SO question: http://stackoverflow.com/questions/882214/data-binding-dynamic-data – itsmatt Aug 30 '12 at 16:02
  • tx itsmatt, seems to be the answer... for other it s generally call a propertybag...i didn t know the exact term when i googling – roks nicolas Aug 30 '12 at 21:01

3 Answers3

0

Why can't you add a Name property? You can use the existing indexer as the backing field:

public string Name
{
  get
  {
    return this["Name"];
  }
  set
  {
    this["Name"] = value;
  }
}
Servy
  • 202,030
  • 26
  • 332
  • 449
  • i cannot specify at design time my propertys, it s for a referential with the possibility to update at runtime the metadat without restarting the application – roks nicolas Aug 30 '12 at 15:32
0

You can make your Entity class look like a collection of key-value string pairs by implementing IDictionary<string, string>

(This whole approach is somewhat questionable, but I presume you have a good reason for it, so I won't go into that discussion.)

phoog
  • 42,068
  • 6
  • 79
  • 117
  • If you need to represent the keys as property names rather than as part of a key/value pair, and the property names are not known at design time (as you indicated in another comment), then I suppose you should use `dynamic`. If you use the ExpandoObject for this, note that it also implements IDictionary (`IDictionary` if I recall correctly). – phoog Aug 30 '12 at 16:33
  • i already tried the dynmic but i did'nt found enable the binding mechanism for them – roks nicolas Aug 30 '12 at 16:40
0

You are essentially talking about a list of lists. What I do is nested databound controls. The first control is bound to your List<Entity> then in on OnItemDataBound event pull out the data item and then bind it to your inner control.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
  • Is there anyway to surimplement the Binding mechanism, I didn t find any interface in the framework that i can use to specify this stuff – roks nicolas Aug 30 '12 at 15:33