BindingList with Collection support for binding to a DataGridViewComboBox Column
-
Wednesday, January 11, 2012 11:13 PM
I found this earlier thread on attempting to bind data to a DataGridView containing a ComboBox Column:
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
-
Thursday, January 12, 2012 6:23 AMModerator
It seems that it is not support the StringCollection type as the data source for the DataGridViewComboBoxColumn, I used the IList type property make a demo, you can download it and research.
The package also have a video demonstrates how I bound them with Visual Studio Designer, for codes behind, you can look into the Form1.Designer.cs file:
https://skydrive.live.com/embed?cid=BB789F72272D4858&resid=BB789F72272D4858%21692
https://skydrive.live.com/embed?cid=BB789F72272D4858&resid=BB789F72272D4858%21691
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
- Marked As Answer by Mike Dos ZhangMicrosoft Contingent Staff, Moderator Monday, January 16, 2012 7:10 AM
- Unmarked As Answer by vs2010junkie Tuesday, January 17, 2012 6:07 PM
- Marked As Answer by Mike Dos ZhangMicrosoft Contingent Staff, Moderator Monday, January 23, 2012 7:10 AM
-
Tuesday, January 17, 2012 6:08 PM
The links are not working correctly for me.
Do you think you can verify that the links work correctly?
Thanks.
-
Thursday, January 19, 2012 8:51 AMModerator
Oh, sorry for that, this folder is new, and I forgot to share it to everyone.
You can access it now.
Then let me know the result, thanks!
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us


