jump to navigation

Data binding with generic dictionary objects January 30, 2009

Posted by gcorbin in .NET, ASP.NET, C#.
Tags: , ,
2 comments

This issue caused me some pain recently. So, after I spent some time to figure out how to do this, I figured it would be good to share it with the community. The problem is that I had to created my own class called Item. This class was meant to contain all properties and methods on an Item object. I then wanted to have a collection of Item objects, so I created a generic dictionary that contained the Item object as the Kvalue for the generic dictionary. The problem comes when you try to databind this collection to a control.

 

Many of the WinForm controls only support databinding to an IList. To make the generic dictionary work here, we need to use the BindingSource Object (BindingSource implements IList). The next trick is that the DisplayMember needs to be set to a string “Value” and the ValueMember set to a string “Key”. However, “Value” will attempt to display the Item object as a string, so to get around this, we need to override the ToString method of the Item object to display the property of that class. It’s easiest to understand this by seeing a sample.

 

 

private void Form1_Load(object sender, EventArgs e)

{

  //Create a Generic Dictionary.

  Dictionary<int,Item> Items = new Dictionary<int,Item>();

 

  //Load the dictionary

  Items.Add(1, new Item(“FirstItem”, 1));

  Items.Add(2, new Item(“SecondItem”, 2));

  Items.Add(3, new Item(“ThirdItem”, 3));

  Items.Add(4, new Item(“FourthItem”, 4));

 

  //Bind the generic dictionary

  comboBox1.DataSource = new BindingSource(Items, null);

 

  //For this to work, the Item.cs class needs to

  //override the ToString() method.

  comboBox1.DisplayMember = “Value”;

  comboBox1.ValueMember = “Key”;

} 

 

 

class Item

    {

        private string _itemName;

        private int _itemId;

 

        //constructor

        public Item(string strItemName, int itemId)

        {

            this._itemName = strItemName;

            this._itemId = itemId;

        }

 

        public string ItemName

        {

            get{ return _itemName; }

        }

 

 

        public int ItemId

        {

            get{ return _itemId; }

        }

 

        //Override ToString to the itemName property.

        public override string ToString()

        {

            return _itemName;

        }

    }

 

 

 

The compelte source code for this demo can be downloaded here.

 

Enjoy.

 

Advertisements

Going into the (Silver)Light. April 29, 2008

Posted by gcorbin in ASP.NET, Silverlight.
Tags: , ,
4 comments

By now everyone has seen Microsoft’s latest web technology, Silverlight. If you haven’t you got to go check it out. Many blogs I’ve seen have compared it to Macromedia Flash. But, I’ve got to tell you that it differences are just as great as its similarities to Flash. I’ve done work in Flash in the past and it is a great technology. But, Silverlight is in position to become new technology to dominate the client-side browser. There are a few reasons for this.

 

First, the learning curve for Silverlight is much lower for developers that already understand how to use the .Net Framework. Even thou Silverlight 1.0 only supports javascript , the XAML syntax is exactly the same as if you were creating a desktop WPF application. Once Silverlight 2.0 is out, the learning curve will be even less as that version will support managed C#.

 

Second, the IDE for creating Silverlight applications is Visual Studio. This is the same development tool that many developers use for building other windows applications.

 

Third, the event model in Silverlight is the same as ASP.NET. Flash uses an event model that is based on a storyboard that repeats on a specified interval. Silverlight has the typical OnLoad, OnClick, etc. In addition, Silverlight does have a storyboard control that you can use if you need the events to all fire as in Flash.

 

After doing a fare amount of research into Silverlight 1.0, I decided it give it a try. I wanted to see how well, if at all, it could integrate with my existing ASP.NET site. I found that it ties in very easily. For ease of use, I created an ASP.NET Server control that embeds all the client-side JS files and XAML files needed for my Silverlight application. Then all that I needed to do was to drop that server control on my ASP.NET page and set a few parameters.

 

My Silverlight app is very simple. I decided to make use of the WPF Ink class to create a Silverlight Signature Control. You can check it a live demo and find the source code for it here. One of the nice things about this control is that is demonstrates several things. With this sample you can see to following:

 

  • Creating an ASP.Net Server control
  • Working with embedded JavaScript resources
  • Embedding XAML for use with Silverlight.
  • Creating a Silverlight application

 

Enjoy.

Microsoft AJAX.Net V1.0 ??? February 25, 2007

Posted by gcorbin in ASP.NET.
add a comment

I’ve been playing with this technology long before the term AJAX was ever coined. Even before the XML DOM came around and gave us XmlHttpRequest, there were ways to accomplish the same effect using iframes. Lately, I’ve been experimenting with MS AJAX.Net and I must admit, its very nice. Many of the details of making Ajax calls are hidden away. All encapsulated in this new control library. I can help but to think that Microsoft may have missed the boat on this one. Google has been offering Ajax type controls for over a year now, and I’ve personally have been using 3rd party controls such as Telerik and Nitobi web controls that have loads of Ajax functionality build in. I guess time will tell how this plays out and what control set will become more dominate on the web. For now, I think I’ll stick with what works for me and let the web figure out the rest.