本文介绍了将文本列转换为对象绑定DataGridView控件中的组合框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个名为Contacts的类,它包含一组名为PhoneNumbers的对象。每个PhoneNumbers对象都有几个属性,包括PhoneNumber和PhoneType以及外键PhoneTypeFK。我将此集合分配给DataGridView控件的DataSource,并隐藏我不希望在DGV中看到的所有其他列(属性)。这会在DGV中创建一组文本单元格(行/列),用于电话号码和类型。一切都很好,除了,我想在电话类型列中显示一个组合框,其中填充了PhoneTypes表的所有各种电话类型,并在该电话号码的单元格中显示相应的电话类型。 我在某处读到需要在设计时添加一个组合框列,其中DataPropertyName与PhoneNumbers对象的属性相同,即PhoneType,这样当DGV填充列时,它将使用此列而不是创建新列(对于PhoneType)。但是,我无法让它发挥作用。我使用下面的代码填充DGV并隐藏不相关的列: / / 填充网格 uxContactPhoneNumbersGrd.DataSource = contacts.PhoneNumbers; // 隐藏不需要的列/行 uxContactPhoneNumbersGrd .RowHeadersVisible = false ; string [] columnNamesToHide = { ErrMsg , ContactsFk, PhoneNumbersPk, PhoneTypesFk}; SAPSCommon.Instance.HideGridColumns( ref uxContactPhoneNumbersGrd,columnNamesToHide); 当我这样做时,我为PhoneType获得了2列,一个是填充DGV时创建的文本单元格,另一个是我在设计时添加的组合框列(即使它具有与建议相同的DataPropertyName)。 如何仅为PhoneType获取1列,如何将其绑定到PhoneTypes表,以便来自PhoneNumbers Objects的数据为相应的PhoneNumber设置正确的PhoneType? (在使用PhoneNumbers对象填充网格之前,我是否需要首先绑定PhoneType组合框列?)解决方案 I have a class called Contacts which contains a collection of objects called PhoneNumbers. Each PhoneNumbers object has several properties including PhoneNumber and PhoneType as well as the foreign key PhoneTypeFK. I assign this collection to a DataGridView control’s DataSource and hide all the other columns (properties) I don’t want to see in the DGV. This creates a bunch of text cells (rows/cols) in the DGV being for the Phone Number and the Type. All well and good except, I want a combobox to be shown in the Phone Type column populated with all the various Phone Types of the PhoneTypes Table with the appropriate Phone Type shown in the cell for that Phone Number.I read somewhere that I need to add a combobox column at design time with the same DataPropertyName as the Property of the PhoneNumbers Object, i.e. PhoneType, so that when the DGV is populated with columns it will use this column instead of creating a new one (for the PhoneType). I cannot, however, get this to work. I used the code below to populate the DGV and hide the irrelevant columns://Fill GriduxContactPhoneNumbersGrd.DataSource = contacts.PhoneNumbers;//Hide non-required columns/rowsuxContactPhoneNumbersGrd.RowHeadersVisible = false;string[] columnNamesToHide = { "ErrMsg", "ContactsFk", "PhoneNumbersPk", "PhoneTypesFk" };SAPSCommon.Instance.HideGridColumns(ref uxContactPhoneNumbersGrd, columnNamesToHide);When I do this, I get 2 columns for the PhoneType, one is the text cell created when populating the DGV, the other is the combobox column I added at design time (even though it has the same DataPropertyName as suggested).How do I get only 1 column for PhoneType and how do I bind it to the PhoneTypes Table so that the data from the PhoneNumbers Objects sets the correct PhoneType for the respective PhoneNumber? (Do I need to bind the PhoneType combobox column 1st before populating the grid with the PhoneNumbers Objects?) 解决方案 这篇关于将文本列转换为对象绑定DataGridView控件中的组合框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 02:58