0

Story: We have 3 different tables. TimeReport, Dossier, BU The first one is connected to the second one, one to many. The second one is connected to the third one, one to many. see picture db.png

We wanted to use a radgrid component to display 'TimeReports'. We built in filters for example for 'DossierId' and are displaying the property 'Description'. This works fine.

Our problem is now that we want to display and filter for example BU.BUId and BU.Code. We tried this several ways, with java script or in code behind. Unfortunately it doesn't work. The problem seems to be that we want access the properties over two tables. (from 'TimeReport' over 'Dossier' to 'BU')

    <telerik:GridBoundColumn DataField="Dossier.BU.Code" FilterControlAltText="Filter BUId column" 
    HeaderText="BU" SortExpression="BUId" UniqueName="BUId"> 
    <FilterTemplate> 
        <telerik:RadComboBox ID="RadComboBoxTitle" DataSourceID="dsBU" DataTextField="Code" 
            DataValueField="BUId" AppendDataBoundItems="true" AutoPostBack="true" 
            OnPreRender="RadComboBoxTitle_PreRender" 
            runat="server" OnSelectedIndexChanged="RadComboBoxTitle_SelectedIndexChanged"> 
            <Items> 
                <telerik:RadComboBoxItem Text="All" /> 
            </Items> 
        </telerik:RadComboBox> 
    </FilterTemplate> 
</telerik:GridBoundColumn> 

Code behind

protected void RadComboBoxTitle_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e) 
{ 
    RadComboBox buCombo = sender as RadComboBox; 

    ViewState["buComboValue"] = buCombo.SelectedValue; 

    TimeReportGrid.MasterTableView.FilterExpression = "(it.[Dossier.BUId] = " + buCombo.SelectedValue + ")"; 
    GridColumn column = TimeReportGrid.MasterTableView.GetColumnSafe("BUId"); 
    column.CurrentFilterFunction = GridKnownFunction.EqualTo; 
    column.CurrentFilterValue = buCombo.SelectedValue; 
    TimeReportGrid.Rebind(); 
} 
protected void RadComboBoxTitle_PreRender(object sender, EventArgs e) 
{ 
    if (ViewState["buComboValue"] != null) 
    { 
        RadComboBox buCombo = sender as RadComboBox; 
        buCombo.SelectedValue = ViewState["buComboValue"].ToString(); 
    } 
} 

This Code throws an error on "TimeReportGrid.Rebind()".

'Dossier.BUId' is not a member of type 'Model.TimeReport' in the currently loaded schemas. Near escaped identifier, line 6, column 5.

Feel free to ask questions if necessary.

Thanks for your help and fast answer.

4

1 回答 1

1

看起来您遇到了一个问题,当您过滤和重新绑定时,您的 EntityFramework 对象与数据库断开连接,并且它们无法延迟加载您尝试过滤的内容。尝试在加载网格的初始查询中使用Include()语句,以便您想要过滤的表在正在过滤的断开连接的集合中可用。


代码示例 - 具有以下 Code-First Entity Framework 对象:

public class MyContext : DbContext
{

    public DbSet<TimeReport> TimeReport { get; set; }

    public DbSet<Dossier> Dossier { get; set; }

    public DbSet<BU> BU { get; set; }

}

public class BU
{

    public int BUId { get; set; }

    public string Code { get; set; }

}

public class Dossier
{

    public int DossierId { get; set; }

    [ForeignKey("BU")]
    public int BUId { get; set; }

    public BU BU { get; set; }

}

public class TimeReport
{

    public int TimeReportId { get; set; }

    [ForeignKey("Dossier")]
    public int DossierId { get; set; }

    public Dossier Dossier { get; set; }

}

我能够在 RadGrid 的 MasterTableView 上重写您的 FilterExpression 以:

TimeReportGrid.MasterTableView.FilterExpression = "(it.Dossier.BUId = " + buCombo.SelectedValue + ")";

并且该连接有效。

查看此链接以获取有关如何为RadGrid自定义 FilterExpression的更多信息

于 2013-05-03T15:06:22.660 回答