BindingList with Collection support for binding to a DataGridViewComboBox Column

คำตอบ BindingList with Collection support for binding to a DataGridViewComboBox Column

  • Wednesday, January 11, 2012 11:13 PM
     
      Has Code

    I found this earlier thread on attempting to bind data to a DataGridView containing a ComboBox Column:

    http://social.msdn.microsoft.com/forums/en-us/winformsdatacontrols/thread/23DD994F-7D3D-434B-8EF3-1A0F06A3437A

    However, the solution that was provided is not exactly what I want.  

    I want to achieve a similar solution provided to the ObjectDataSource whereby I can expose either a method or set of properties that can directly bind to the DataGridView control and map each of the properties to the appropriate columns in the DataGridView.

    Below is the code for the BindingList class that I am creating:

    
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Diagnostics;
    using System.Windows.Forms;
    using System.Linq;
    using System.Xml.Linq;
    
    using Microsoft.IdentityModel.Protocols.WSIdentity;
    using System.ComponentModel;
    using System.Collections.Specialized;
    // DisplayClaimsCollection doesn't support databinding and we have to implement a new collection
    namespace STSFederationMetadataEditor
    {
    	public class CustomDisplayClaimCollection : BindingList<CustomDisplayClaim>
    	{
    
    	}
    
    //DisplayClaim has a readonly ClaimType property
    	public class CustomDisplayClaim
    	{
            private StringCollection m_ClaimTypeCollection;
            public int selectedClaim
            {
                get
                {
                    return m_ClaimTypeCollection.IndexOf(ClaimType);
                }//get
                set
                {
                    if (!m_ClaimTypeCollection.Contains(ClaimType))
                    {
                        m_ClaimTypeCollection.Add(ClaimType);
                    }//if
                }//set
            }
    
            private string ClaimType
            {
                get;
                set;
            }
            public StringCollection ClaimTypeCollection
            {
                get
                {
                    return m_ClaimTypeCollection;
                }//get
                set
                {
                    if (!m_ClaimTypeCollection.Contains(ClaimType))
                    {
                        m_ClaimTypeCollection.Add(ClaimType);
                    }//if
                }//set
            }
    		public string displayTag {get; set;}
    		public string description {get; set;}
    		public string displayValue {get; set;}
    		public bool optional {get; set;}
    
            /// <summary>
            /// Class constructor
            /// </summary>
            public CustomDisplayClaim(string claimType)
            {
                this.ClaimType = claimType;
                InitializeClaimTypes();
            }//constructor
    
            public void InitializeClaimTypes()
            {
                string[] claimTypes =
                    new[] 
                    {
                        "[Pick or type]",
                        "http://schemas.xmlsoap.org/ws/2009/09/identity/claims/actor",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/anonymous",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/authentication",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationinstant",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationmethod",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/authorizationdecision",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims",
                        "http://schemas.xmlsoap.org/ws/2009/09/identity/claims",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/cookiepath",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/country",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/denyonlyprimarygroupsid",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/denyonlyprimarysid",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/denyonlysid",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dns",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/dsa",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/expiration",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/expired",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/gender",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/hash",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/homephone",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/ispersistent",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/mobilephone",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/otherphone",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/postalcode",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/primarygroupsid",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/primarysid",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/rsa",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/serialnumber",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/sid",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/spn",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/stateorprovince",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/streetaddress",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/system",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/thumbprint",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/uri",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/userdata",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/version",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/webpage",
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/windowsaccountname",
                        "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/x500distinguishedname",
                        "http://schemas.xmlsoap.org/claims",
                        "http://schemas.xmlsoap.org/claims/CommonName",
                        "http://schemas.xmlsoap.org/claims/EmailAddress",
                        "http://schemas.xmlsoap.org/claims/Group",
                        "http://schemas.xmlsoap.org/claims/UPN"
                };
    
                m_ClaimTypeCollection = new StringCollection();
                m_ClaimTypeCollection.AddRange(claimTypes);
            }
    
    		public DisplayClaim GetDiplayClaim()
    		{
    			return new DisplayClaim(ClaimType, displayTag, description, displayValue, optional);
    		}
    
    		public static CustomDisplayClaim FromDisplayClaim(ref DisplayClaim claim)
    		{
    			return new CustomDisplayClaim(claim.ClaimType) {description = claim.Description, displayTag = claim.DisplayTag, displayValue = claim.DisplayValue, optional = claim.Optional};
    		}
    
    	}
    
    }
    


    I basically want the collection property/field bound to the ComboBox column.  In addition, if there is a value that needs to be loaded into the ComboBox column, I would like to match the SelectedIndex from amongst the items in the collection and then perform the binding.

    Please let me know how to achieve this.

    Thanks.

All Replies