无法将类型'system.reflection.fieldinfo'隐式转换为'devexpress.dataaccess.excel.fieldinfo'

时间:2019-03-25 15:03:36

标签: c# devexpress

Error Image

  

在此先感谢大家。...我正在使用devexpress xtragridcontrol   根据行高显示数据。但是有一个问题   无法将类型system.reflection.fieldinfo隐式转换为   devexpress.dataaccess.excel.fieldinfo,有什么建议...

     

这是我的代码

private void UpdateGridSize()
{
    GridViewInfo viewInfo = (GridViewInfo)gridView2.GetViewInfo();
    DevExpress.DataAccess.Excel.FieldInfo fi = typeof(GridView).GetField("scrollInfo", BindingFlags.Instance | BindingFlags.NonPublic);
    ScrollInfo scrollInfo = (ScrollInfo)fi.GetValue(gridView2);
    int width = viewInfo.ViewRects.IndicatorWidth;
    foreach (GridColumn column in gridView1.VisibleColumns)
    {
        if (viewInfo.GetColumnLeftCoord(column) < viewInfo.ViewRects.ColumnPanelWidth)
            gridView1.LeftCoord = width;
        width += viewInfo.ColumnsInfo[column].Bounds.Width;
    }
    if (scrollInfo.VScrollVisible) width += scrollInfo.VScrollSize;
    int height = viewInfo.CalcRealViewHeight(new Rectangle(0, 0, ClientSize.Width, ClientSize.Height), true);
    if (scrollInfo.HScrollVisible) height += scrollInfo.HScrollSize;
    width = Math.Max(GridMinWidth, width);
    width = Math.Min(ClientSize.Width - gridControl1.Location.X, width);
    height = Math.Max(GridMinHeight, height);
    height = Math.Min(ClientSize.Height - gridControl1.Location.Y, height);
    gridControl1.Size = new Size(width, height);
    gridView1.LayoutChanged();
}

1 个答案:

答案 0 :(得分:1)

反射类型和DevExpress FieldInfo类型无关,因此不兼容分配。创建一个新对象并分配值

var fieldInfo = typeof(GridView).GetField("scrollInfo",
    BindingFlags.Instance | BindingFlags.NonPublic); // Reflection

var fi = new DevExpress.DataAccess.Excel.FieldInfo {
    Name = fieldInfo.Name, Type = fieldInfo.MemberType
};

还是您要执行以下操作:

viewInfo.GetField(...)

代替

typeof(GridView).GetField(

根据DevExpress the class GridViewInfo is not documented and subject to changes

  

该类未记录,因为它描述了内部可视化网格表示,可以根据开发要求动态更改它。我们倾向于防止客户使用此类。