【问题标题】:Using an attribute to specify a columns FillWeight使用属性指定列 FillWeight
【发布时间】:2017-06-14 23:43:01
【问题描述】:

我有一个使用 BindingList 填充的 DataGridView。 BindingList 是一个SyncDetail 类型的列表,它有许多属性。在这些属性中,我可以使用属性来决定是否不显示列(Browsable(false))、列的显示名称(DisplayName("ColumnName"))等。示例如下:

public class SyncDetail : ISyncDetail
{

    // Browsable properties

    [DisplayName("Sync Name")]
    public string Name { get; set; }

    // Non browsable properties

    [Browsable(false)]
    [XmlIgnore]
    public bool Disposed { get; set; }
}

有没有办法可以使用属性来定义应该设置的列宽?例如[ColumnWidth(200)]。如果可能,我想设置FillWeight,因为我的AutoSizeColumnsMode 设置为Fill

谢谢。

【问题讨论】:

  • 你想要一个属性来为你的属性设置最大长度?

标签: c# datagridview attributes datagridviewcolumn


【解决方案1】:

我最终实现了一个自定义属性来执行此操作。

public class ColumnWeight : Attribute
    {
        public int Weight { get; set; }

        public ColumnWeight(int weight)
        {
            Weight = weight;
        }
}

然后我可以重写 DataGridView 的 OnColumnAdded 方法来获取属性并设置列的 FillWeight。

protected override void OnColumnAdded(DataGridViewColumnEventArgs e)
{
    // Get the property object based on the DataPropertyName of the column
    var property = typeof(SyncDetail).GetProperty(e.Column.DataPropertyName);
    // Get the ColumnWeight attribute from the property if it exists
    var weightAttribute = (ColumnWeight)property.GetCustomAttribute(typeof(ColumnWeight));
    if (weightAttribute != null)
    {
        // Finally, set the FillWeight of the column to our defined weight in the attribute
        e.Column.FillWeight = weightAttribute.Weight;
    }
    base.OnColumnAdded(e);
}

然后我可以在我的对象的属性上设置属性。

public class SyncDetail : ISyncDetail
{

    // Browsable properties

    [DisplayName("Sync Name")]
    [ColumnWeight(20)]
    public string Name { get; set; }

    etc...
}

【讨论】:

    【解决方案2】:

    根据您的原始答案,我做了一个稍微扩展的用例,可以与绑定到不同 Column 属性的多个数据网格一起使用,这些属性以整数值作为后盾。

    在 SyncDetails 类中使用 Width 和 MinimumWidth 属性的示例:

        [ColumnProperty("MinimumWidth",100)]
        [ColumnProperty("Width",250 )]
        public string Name{get;set;}
    

    然后我的属性定义被更新为允许多个属性:

    [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
    public class ColumnPropertyAttribute:Attribute
    {
        public string ColumnPropertyName { get; set; }
        public int ColumnValue { get; set; }
    
        public ColumnPropertyAttribute(string columnPropertyName, int columnValue)
        {
            ColumnValue = columnValue;
            ColumnPropertyName = columnPropertyName;
        }
    }
    

    在 _ColumnAdded 事件中,我们查找数据网格绑定到的任何类型,从中提取自定义属性,然后尝试应用该值。我将所有内容绑定到 BindingList<T>,因此 .GetGenericArguments() 返回该类型 T 用于自定义属性查找,但可以根据其他应用程序的需要进行调整。

    private void DataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
    {
        try
        {
            var propInfo = (sender as DataGridView).DataSource.GetType().GetGenericArguments()[0].GetProperty(e.Column.DataPropertyName);
            if (propInfo != null)
            {
                var customAttributes = propInfo.GetCustomAttributes<ColumnPropertyAttribute>();
                foreach (var ca in customAttributes)
                {
                    var column = e.Column;
                    var propToChange = e.Column.GetType().GetProperty(ca.ColumnPropertyName);
                    propToChange.SetValue(column, ca.ColumnValue);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception processing custom _ColumnAdded properties: " + ex.Message);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      相关资源
      最近更新 更多