【问题标题】:Choosing column from other table/object从其他表/对象中选择列
【发布时间】:2009-08-10 08:47:13
【问题描述】:

我有一个 DataGridView,它的 DataSource 连接到对象。一切正常(我可以编辑该对象并保留更改),直到我想选择使用组合框连接对象(在数据库中它通过外键连接)。

我可以填充组合框,以便显示正确的值(即用户名)。我通过选择该特定列的数据源来做到这一点。它允许我选择显示的属性以及值(但我不能将值设置为整个选择的对象)。但是,在保存对象时,会发生错误。 DataGridView 正在尝试保存所选对象的值(此“外部对象”的属性),而不是对象本身。

如何说服 DataGridView 保存所选对象,而不仅仅是其属性之一?

【问题讨论】:

    标签: c# datagridview combobox


    【解决方案1】:

    这是常见问题之一。您不能使用原始 DataGridViewComboboxColumn 执行此操作。我前段时间找到了一个解决方案 - 你必须对其进行子类化。代码如下:

       public class DataGridViewBusinessComboBoxColumn : DataGridViewComboBoxColumn
        {
            public DataGridViewBusinessComboBoxColumn()
            {
                CellTemplate = new DataGridViewBusinessComboBoxCell();
            }
        }
    
        public class DataGridViewBusinessComboBoxCell : DataGridViewComboBoxCell
        {
            private System.ComponentModel.PropertyDescriptor displayProp;
    
            private CurrencyManager ListManager
            {
                get
                {
                    BindingMemberInfo bmi = new BindingMemberInfo(base.DisplayMember);
                    if (DataGridView != null)
                    {
                        return (CurrencyManager)
                               DataGridView.BindingContext[DataSource, bmi.BindingPath];
                    }
                    return null;
                }
            }
    
            private System.ComponentModel.PropertyDescriptor DisplayProp
            {
                get
                {
                    if (displayProp == null)
                    {
                        displayProp = ListManager.GetItemProperties().Find(DisplayMember,
                                                                           true);
                    }
                    return displayProp;
                }
            }
    
            protected override object GetFormattedValue(object value, int rowIndex,
                                                        ref DataGridViewCellStyle cellStyle,
                                                        TypeConverter valueTypeConverter,
                                                        TypeConverter formattedValueTypeConverter,
                                                        DataGridViewDataErrorContexts
                                                            context)
            {
                if (value == null || value == cellStyle.DataSourceNullValue)
                    return "";
    
                return base.GetFormattedValue(DisplayProp.GetValue(value),
                                              rowIndex, ref cellStyle, valueTypeConverter,
                                              formattedValueTypeConverter, context);
            }
    
            public override object ParseFormattedValue(object formattedValue,
                                                       DataGridViewCellStyle cellStyle,
                                                       TypeConverter formattedValueTypeConverter,
                                                       TypeConverter valueTypeConverter)
            {
                foreach (object item in ListManager.List)
                {
                    if ((string) DisplayProp.GetValue(item) == (string) formattedValue)
                        return item;
                }
    
                return base.ParseFormattedValue(formattedValue, cellStyle,
                                                formattedValueTypeConverter, valueTypeConverter);
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 2021-01-09
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多