BindingList with Collection support for binding to a DataGridViewComboBox Column

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
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 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:

<div style="color:Black;background-color:White; <pre>

<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Diagnostics;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Xml.Linq;

<span style="color:Blue; using Microsoft.IdentityModel.Protocols.WSIdentity;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Collections.Specialized;
<span style="color:Green; // DisplayClaimsCollection doesnt support databinding and we have to implement a new collection
<span style="color:Blue; namespace STSFederationMetadataEditor
{
<span style="color:Blue; public <span style="color:Blue; class CustomDisplayClaimCollection : BindingList<CustomDisplayClaim>
{

}

<span style="color:Green; //DisplayClaim has a readonly ClaimType property
<span style="color:Blue; public <span style="color:Blue; class CustomDisplayClaim
{
<span style="color:Blue; private StringCollection m_ClaimTypeCollection;
<span style="color:Blue; public <span style="color:Blue; int selectedClaim
{
<span style="color:Blue; get
{
<span style="color:Blue; return m_ClaimTypeCollection.IndexOf(ClaimType);
}<span style="color:Green; //get
<span style="color:Blue; set
{
<span style="color:Blue; if (!m_ClaimTypeCollection.Contains(ClaimType))
{
m_ClaimTypeCollection.Add(ClaimType);
}<span style="color:Green; //if
}<span style="color:Green; //set
}

<span style="color:Blue; private <span style="color:Blue; string ClaimType
{
<span style="color:Blue; get;
<span style="color:Blue; set;
}
<span style="color:Blue; public StringCollection ClaimTypeCollection
{
<span style="color:Blue; get
{
<span style="color:Blue; return m_ClaimTypeCollection;
}<span style="color:Green; //get
<span style="color:Blue; set
{
<span style="color:Blue; if (!m_ClaimTypeCollection.Contains(ClaimType))
{
m_ClaimTypeCollection.Add(ClaimType);
}<span style="color:Green; //if
}<span style="color:Green; //set
}
<span style="color:Blue; public <span style="color:Blue; string displayTag {<span style="color:Blue; get; <span style="color:Blue; set;}
<span style="color:Blue; public <span style="color:Blue; string description {<span style="color:Blue; get; <span style="color:Blue; set;}
<span style="color:Blue; public <span style="color:Blue; string displayValue {<span style="color:Blue; get; <span style="color:Blue; set;}
<span style="color:Blue; public <span style="color:Blue; bool optional {<span style="color:Blue; get; <span style="color:Blue; set;}

<span style="color:Gray; /// <span style="color:Gray; <summary>
<span style="color:Gray; ///<span style="color:Green; Class constructor
<span style="color:Gray; /// <span style="color:Gray; </summary>
<span style="color:Blue; public CustomDisplayClaim(<span style="color:Blue; string claimType)
{
<span style="color:Blue; this.ClaimType = claimType;
InitializeClaimTypes();
}<span style="color:Green; //constructor

<span style="color:Blue; public <span style="color:Blue; void InitializeClaimTypes()
{
<span style="color:Blue; string[] claimTypes =
<span style="color:Blue; new[]
{
<span style="color:#A31515; "[Pick or type]",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2009/09/identity/claims/actor",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/anonymous",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/authentication",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationinstant",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationmethod",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/authorizationdecision",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2009/09/identity/claims",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/cookiepath",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/country",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/denyonlyprimarygroupsid",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/denyonlyprimarysid",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/denyonlysid",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dns",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/dsa",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/expiration",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/expired",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/gender",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/hash",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/homephone",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/ispersistent",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/mobilephone",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/otherphone",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/postalcode",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/primarygroupsid",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/primarysid",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/rsa",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/serialnumber",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/sid",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/spn",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/stateorprovince",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/streetaddress",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/system",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/thumbprint",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/uri",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/userdata",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/version",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/webpage",
<span style="color:#A31515; "http://schemas.microsoft.com/ws/2008/06/identity/claims/windowsaccountname",
<span style="color:#A31515; "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/x500distinguishedname",
<span style="color:#A31515; "http://schemas.xmlsoap.org/claims",
<span style="color:#A31515; "http://schemas.xmlsoap.org/claims/CommonName",
<span style="color:#A31515; "http://schemas.xmlsoap.org/claims/EmailAddress",
<span style="color:#A31515; "http://schemas.xmlsoap.org/claims/Group",
<span style="color:#A31515; "http://schemas.xmlsoap.org/claims/UPN"
};

m_ClaimTypeCollection = <span style="color:Blue; new StringCollection();
m_ClaimTypeCollection.AddRange(claimTypes);
}

<span style="color:Blue; public DisplayClaim GetDiplayClaim()
{
<span style="color:Blue; return <span style="color:Blue; new DisplayClaim(ClaimType, displayTag, description, displayValue, optional);
}

<span style="color:Blue; public <span style="color:Blue; static CustomDisplayClaim FromDisplayClaim(<span style="color:Blue; ref DisplayClaim claim)
{
<span style="color:Blue; return <span style="color:Blue; new CustomDisplayClaim(claim.ClaimType) {description = claim.Description, displayTag = claim.DisplayTag, displayValue = claim.DisplayValue, optional = claim.Optional};
}

}

}
[/code]

<br/>
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.

View the full article
 
Back
Top