Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Databinding a ListBox to List

Databinding a ListBox to List

Scheduled Pinned Locked Moved ASP.NET
csharpasp-nethelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JambeDuSinge
    wrote on last edited by
    #1

    Hi The gist of my problem is that I want to bind an ASP.NET ListBox to a List< T > and have the Value field populated by a property of an object in the List and the Text field populated by the result of the call ToString() on the object in the List (the format of which is dictated by the DataTextFormatString propery of the ListBox): I have two classes PersonInfo - has properties PersonId / FirstName / LastName. This class implements IFormattable allowing customized string representation of any instantiated object. PersonInfoList which derives from List< PersonInfo > (so stores objects of type PersonInfo). I want to use PersonInfoList as the DataSource to a ListBox: PersonInfoList pil = new PersonInfoList(); ListBox1.DataSource = pil; ListBox1.DataTextFormatString = "{0:l, f}"; //formats output as [LastName], [FirstName] ListBox1.DataBind(); By not specifying the DataTextField or DataValue field, the IFormattable ToString() method is called on each PersonInfo object in the PersonInfoList and both the Text and Value properties are populated with [LastName], [FirstName] as expected. The problem is that I want the Value field to contain PersonId and the Text field to contain the Person's name (formatted) as above. If I add PersonId to the ListBox1.DataValueField and do not specify a value for ListBox1.DataTextField, the PersonId is used for both Text and Value. The only way that I have found around this is to add a Me property the PersonInfo class: public PersonInfo Me { get{ return this; } } and then set ListBox1.DataTextField = "Me"; YUCK! This produces the desired output, but there has to be a better way to do this! :confused: Any ideas would be much appreciated. Chris

    M 1 Reply Last reply
    0
    • J JambeDuSinge

      Hi The gist of my problem is that I want to bind an ASP.NET ListBox to a List< T > and have the Value field populated by a property of an object in the List and the Text field populated by the result of the call ToString() on the object in the List (the format of which is dictated by the DataTextFormatString propery of the ListBox): I have two classes PersonInfo - has properties PersonId / FirstName / LastName. This class implements IFormattable allowing customized string representation of any instantiated object. PersonInfoList which derives from List< PersonInfo > (so stores objects of type PersonInfo). I want to use PersonInfoList as the DataSource to a ListBox: PersonInfoList pil = new PersonInfoList(); ListBox1.DataSource = pil; ListBox1.DataTextFormatString = "{0:l, f}"; //formats output as [LastName], [FirstName] ListBox1.DataBind(); By not specifying the DataTextField or DataValue field, the IFormattable ToString() method is called on each PersonInfo object in the PersonInfoList and both the Text and Value properties are populated with [LastName], [FirstName] as expected. The problem is that I want the Value field to contain PersonId and the Text field to contain the Person's name (formatted) as above. If I add PersonId to the ListBox1.DataValueField and do not specify a value for ListBox1.DataTextField, the PersonId is used for both Text and Value. The only way that I have found around this is to add a Me property the PersonInfo class: public PersonInfo Me { get{ return this; } } and then set ListBox1.DataTextField = "Me"; YUCK! This produces the desired output, but there has to be a better way to do this! :confused: Any ideas would be much appreciated. Chris

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, When a control is not suitable for my need, I might think of customizing the control. So IMHO, in this case you might want to create a custom ListBox control by inheriting from the standard ListBox control and simply override the PerformDataBinding method which is where you can put your own data binding logic.

      J 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, When a control is not suitable for my need, I might think of customizing the control. So IMHO, in this case you might want to create a custom ListBox control by inheriting from the standard ListBox control and simply override the PerformDataBinding method which is where you can put your own data binding logic.

        J Offline
        J Offline
        JambeDuSinge
        wrote on last edited by
        #3

        Hi Thanks for the repsonse. Just strikes me as odd that this functionality isn't available out of the box (it very nearly is - just not quite. I discovered that I can get the functionality by using Dictionary< T > with PersonId as Key and the PersonInfo object as Value (and setting DataTextField="Value" and DataValueField="Key") - but then I can't sort the output by value (as far as I can tell) which is another requirement. Ah well - gives me a good excuse to dig into the PerformDataBinding method I guess! Thanks again for your help. C

        M 1 Reply Last reply
        0
        • J JambeDuSinge

          Hi Thanks for the repsonse. Just strikes me as odd that this functionality isn't available out of the box (it very nearly is - just not quite. I discovered that I can get the functionality by using Dictionary< T > with PersonId as Key and the PersonInfo object as Value (and setting DataTextField="Value" and DataValueField="Key") - but then I can't sort the output by value (as far as I can tell) which is another requirement. Ah well - gives me a good excuse to dig into the PerformDataBinding method I guess! Thanks again for your help. C

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Basically, the ListBox control inherits from the base class ListControl, when data is bound to the control the PerformDataBinding gets executed internally. And this method contains the default logic to build the items collection from the specified data source with the settings like DataTextField, DataValueField and DataTextFormatString. Fortunately, this method is overridable, so you're not happy with the default behavior, you can change it the way you want by overriding this method in your custom control. In addition, you may consider using the generic Dictionary as you discovered only when you bind data to the control. By this I mean, you can sort data with the List generic collection, you then copy it to the Dictionary and bind it to the control. However, this step is unnecessary to me. Adding a public property like Me is also an option, but it's not really a good choice IMHO as it requires you to change your object design.

          J 1 Reply Last reply
          0
          • M minhpc_bk

            Basically, the ListBox control inherits from the base class ListControl, when data is bound to the control the PerformDataBinding gets executed internally. And this method contains the default logic to build the items collection from the specified data source with the settings like DataTextField, DataValueField and DataTextFormatString. Fortunately, this method is overridable, so you're not happy with the default behavior, you can change it the way you want by overriding this method in your custom control. In addition, you may consider using the generic Dictionary as you discovered only when you bind data to the control. By this I mean, you can sort data with the List generic collection, you then copy it to the Dictionary and bind it to the control. However, this step is unnecessary to me. Adding a public property like Me is also an option, but it's not really a good choice IMHO as it requires you to change your object design.

            J Offline
            J Offline
            JambeDuSinge
            wrote on last edited by
            #5

            I agree that the Me property is less than desirable. Interesting point about transferring it to a Dictionary prior to binding. Hadn't thought of that! Ultimately, I'll probably just follow your advice and subclass ListBox and any other controls that I need to use this in. As I said, it strikes me as odd that this isn't available out of the box. It's surely not that unusual a requirement to be able to use a property of an object as the Value field and the ToString() representation of that object as the Text field and to be able to acheive this via data binding. Any way, your feedback has been most useful and has given me something to dig into. So many thanks for your time. C

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups