简体   繁体   English

datagrid列值不显示c#winform

[英]datagrid column value not displaying c# winform

I have a business object that returns a list of objects. 我有一个返回对象列表的业务对象。 This list is then being used as a datasource for a datagridview on a winform. 然后,此列表用作winform上datagridview的数据源。 I load my list, set it to the datasource, refresh and view. 我加载列表,将其设置为数据源,然后刷新并查看。 One of the columns is present but has no values. 列之一存在,但没有值。 I see the header but no data. 我看到标题但没有数据。 The column is for TruckColorId. 该列用于TruckColorId。 Id shows the correct value. ID显示正确的值。 Debugging the code I can see that there are objects in the list with all the properties I expect to see correctly with data, including the field in question. 调试代码,我可以看到列表中存在一些对象,这些对象具有我希望正确看到的所有属性,包括相关字段。

If I select a row and do a foreach loop of the selected row all columns have value but the TruckColorId. 如果我选择一行并对所选行执行foreach循环,则所有列均具有值,但TruckColorId除外。

public class TruckColor : EditBase 
{
   public int TruckColorId { get; set; }
   public override int Id
   {
    get { return TruckColorId; }
    set { TruckColorId = value; }
   }
}

public class EditBase
{
    public virtual int Id { get; set; }
    public string ShortCode { get; set; }        
    public string Description { get; set; }

    public IList<EditBase> GetAll()
    {
        return new List<EditBase>()
        {
            new TruckColor
            {
                Description = "abc",
                Id = 1,
                ShortCode = "A"
            },
            new TruckColor
            {
                Description = "abcd",
                Id = 2,
                ShortCode = "B"
            },
            new TruckColor
            {
                Description = "abcde",
                Id = 3,
                ShortCode = "C"
            },
        };
    }
  }

And here is the code in my form to load the trucks: 这是我的表格中用于装载卡车的代码:

public void InitTrucks()
{
    TruckColor truck = new TruckColor();

    var trucks = truck.GetAll();
    if (trucks.Count() > 0)
    {
        dataGridView1.AutoGenerateColumns = true;                
        dataGridView1.DataSource = trucks;
        dataGridView1.Refresh();
    }
}

try this 尝试这个

var trucks = truck.GetAll();
 if (trucks.Count() > 0)
{
    List<TruckColor> RealTrucks = trucks.Select(x=>(TruckColor)x).ToList();
    dataGridView1.AutoGenerateColumns = true;                
    dataGridView1.DataSource = RealTrucks;
    dataGridView1.Refresh();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM