c# - 自定义 datagridview 列上的数据绑定(bind)问题

标签 c# winforms data-binding datagridview custom-controls

我在将数据绑定(bind)到自定义 DataGridView 列时遇到问题。我创建了根据从数据库中获取的 int 值按星图显示评级的列。
当我通过手动添加行来测试它时,它工作得很好。但是当我将数据绑定(bind)到它时,该值始终为 null

这是 RatingColumn 类的代码:

class RatingColumn : DataGridViewImageColumn
{
    public RatingColumn()
    {
        this.CellTemplate = new RatingCell();
        this.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        this.ValueType = typeof(int);
        this.Name = "Rating";
        this.ImageLayout = DataGridViewImageCellLayout.Stretch;
    }
}

public class RatingCell : DataGridViewImageCell
{
    static Image[] starImages;

    static RatingCell()
    {
        starImages = new Image[11];

        for (int i = 0; i < 11; i++)
            starImages[i] = (Image)Properties.Resources.ResourceManager.
                GetObject("rating_" + i.ToString());
    }

    public RatingCell()
    {
        this.ValueType = typeof(int);
    }

    protected override object GetFormattedValue
         (object value, int rowIndex, ref DataGridViewCellStyle cellStyle, 
          TypeConverter valueTypeConverter, 
          TypeConverter formattedValueTypeConverter, 
          DataGridViewDataErrorContexts context)
    {
        //Here the value is always null
        return value == null ? starImages[0] : starImages[(int)value];
    }

    protected override void Paint
        (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, 
         int rowIndex, DataGridViewElementStates elementState, object value, 
         object formattedValue, string errorText, DataGridViewCellStyle cellStyle,
         DataGridViewAdvancedBorderStyle advancedBorderStyle, 
         DataGridViewPaintParts paintParts)
    {
        Image cellImage = (Image)formattedValue;
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, 
          value, cellImage, errorText, cellStyle, advancedBorderStyle, 
          (paintParts & ~DataGridViewPaintParts.SelectionBackground));
    }
}

我使用 DataProperyName 绑定(bind)数据

RatingColumn col = new RatingColumn();
        col.DataPropertyName = "Rating";

当我将相同的数据绑定(bind)到 DataGridViewTextBoxColumn 时,它工作正常。

最佳答案

        protected override object GetFormattedValue
         (object value , int rowIndex , ref DataGridViewCellStyle cellStyle ,
          TypeConverter valueTypeConverter ,
          TypeConverter formattedValueTypeConverter ,
          DataGridViewDataErrorContexts context) {

              int v = 0;
              if ( value == null || Convert.IsDBNull ( value ) )
                  return starImages[0];

              v = Convert.ToInt32 ( value ) - 1;

              v = v < 0 ? 0 : ( v >= 3 ? 2 : v );

              return starImages[v];
    }

关于c# - 自定义 datagridview 列上的数据绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6164565/

相关文章:

javascript - 如何在 Angular2 中创建多个控件并将其绑定(bind)到一个字段对象?

c# - 无法将 System.Net.Utilities 与 DNX 4.5.1 一起使用

c# - Visual Studio 2019 for .Net Core 3.1 中的 WinForms 设计器窗口

c# - 引发异常后未释放 Boost 共享互斥锁

c# - 组合框 SelectedIndexChanged 事件 : how to get the previously selected index?

c# - Winforms 中禁用的菜单项仍然显示子项

WPF:绑定(bind)到我的自定义控件不会更新

javascript - Angular 数据绑定(bind)不适用于 async/await,但它可以用于 promises

javascript - 如何在 C# 中将 json 值转换为字典并使用字符串值来寻址它们?

vb.net - 在VB中将字符串或base10转换为base 2