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
J

JambeDuSinge

@JambeDuSinge
About
Posts
4
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Code Samples for a Job Interview
    J JambeDuSinge

    I recently was asked to sit a sit a proprietary test at hackerrank [^]. I didn’t get around to it yet but I found that the training section has some good, short challenges that might be a good way of demonstrating your coding style and approach without the tedium of running your own project. Good luck!

    The Lounge career question

  • Databinding a ListBox to List
    J JambeDuSinge

    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

    ASP.NET csharp asp-net help

  • Databinding a ListBox to List
    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

    ASP.NET csharp asp-net help

  • Databinding a ListBox to List
    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

    ASP.NET csharp asp-net help
  • Login

  • Don't have an account? Register

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