DataGridViewCellCollection的属性Item不存在

DataGridViewCellCollection的文档指出,存在一个属性Item,可以使用其索引或字符串名称。当我尝试通过DataGridViewCellCollection对象使用该属性时,编译器会显示错误消息,该对象不包含该属性,为什么?

评论
  • shf939899
    shf939899 回复

    The documentation is a bit misleading. Actually it's not a property Item, but an indexer (see the docs) you can use to access the individual items. The actual definition is

    public System.Windows.Forms.DataGridViewCell this[int index] { get; set; }
    

    If you have an instance of DataGridViewCellCollection, you can use

    DataGridViewCellCollection _collection; // this is a field, irrelevant how it is set
    
    private DataGridViewCell GetCell(int index)
    {
        return _collection[index]; // here we're accessing the item in the cell collection
    }