当行扩展时,RadGrid填充数据源?

时间:2014-02-26 17:31:34

标签: asp.net telerik radgrid

我在RadGrid中有一个嵌套视图,下面是我的代码隐藏。网格正在请求每一行的详细信息,但我希望它只在行实际扩展时请求详细信息?

Private Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As GridNeedDataSourceEventArgs) Handles dataGridMyLeads.NeedDataSource
    If Not e.IsFromDetailTable Then
        Dim DS = (PersonList.GetFilteredLeads(MySession.RiseEID, MySession.RiseClientID, , , MySession.ShowAll)).OrderBy(Function(c) c.LastAction)
        If RouteData.Values("Cat") = "StatusOverdue" Then DS = DS.Where(Function(c) c.NextAction >= Now())
        dataGridMyLeads.DataSource = DS
    End If
End Sub

Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles dataGridMyLeads.PreRender
    If Not Page.IsPostBack Then
        dataGridMyLeads.MasterTableView.Items(0).ChildItem.FindControl("InnerContainer").Visible = True
    End If
End Sub

Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles dataGridMyLeads.ItemCommand
    If (e.CommandName = "RowClick" OrElse e.CommandName = "ExpandCollapse") And TypeOf e.Item Is GridDataItem Then
        DirectCast(e.Item, GridDataItem).ChildItem.FindControl("InnerContainer").Visible = Not e.Item.Expanded
    End If
    If e.CommandName = "RowClick" OrElse e.CommandName = "ExpandCollapse" Then
        e.Item.Expanded = Not e.Item.Expanded
    End If
    If e.CommandName = "RowClick" OrElse e.CommandName = "ExpandCollapse" Then
        Dim item As GridItem
        For Each item In e.Item.OwnerTableView.Items
            If item.Expanded AndAlso Not item Is e.Item Then
                item.Expanded = False
            End If
        Next item
    End If
End Sub

Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles dataGridMyLeads.ItemCreated
    If TypeOf e.Item Is GridNestedViewItem Then
        e.Item.FindControl("InnerContainer").Visible = (DirectCast(e.Item, GridNestedViewItem)).ParentItem.Expanded
    End If
End Sub

1 个答案:

答案 0 :(得分:2)

RadGrid已经有了DetailTables,所以你要做的就是 -

  1. HierarchyDefaultExpanded="false"
  2. HierarchyLoadMode="ServerOnDemand"
  3. Grid - Programmatic Hierarchy Binding

    enter image description here

    实施例

    <telerik:RadGrid runat="server" ID="RadGrid1"
        AutoGenerateColumns="false"
        OnNeedDataSource="RadGrid1_NeedDataSource"
        OnDetailTableDataBind="RadGrid1_DetailTableDataBind">
        <MasterTableView
            DataKeyNames="CountryId"
            HierarchyDefaultExpanded="false"
            HierarchyLoadMode="ServerOnDemand">
            <Columns>
                <telerik:GridBoundColumn
                    DataField="Name"
                    UniqueName="Name"
                    HeaderText="Country">
                </telerik:GridBoundColumn>
            </Columns>
            <DetailTables>
                <telerik:GridTableView 
                    DataKeyNames="StateId" 
                    Name="States" 
                    AutoGenerateColumns="False">
                    <Columns>
                        <telerik:GridBoundColumn
                            DataField="Name"
                            UniqueName="Name"
                            HeaderText="State">
                        </telerik:GridBoundColumn>
                    </Columns>
                </telerik:GridTableView>
            </DetailTables>
        </MasterTableView>
    </telerik:RadGrid>
    

    代码背后

    protected void RadGrid1_NeedDataSource(object source, 
       GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = Countries;
    }
    
    protected void RadGrid1_DetailTableDataBind(object source, 
       GridDetailTableDataBindEventArgs e)
    {
        GridDataItem item = e.DetailTableView.ParentItem;
    
        int countryId = Convert.ToInt32(item.GetDataKeyValue("CountryId"));
    
        e.DetailTableView.DataSource = Countries
            .First(c => c.CountryId == countryId)
            .States;
    }
    
    public class Country
    {
        public int CountryId { get; set; }
        public string Name { get; set; }
    
        public ICollection<State> States { get; set; }
    
        public Country()
        {
            States = new List<State>();
        }
    }
    
    public class State
    {
        public int StateId { get; set; }
        public string Name { get; set; }
    }
    
    private List<Country> Countries
    {
        get
        {
            return new List<Country>
            {
                new Country
                {
                    CountryId = 1,
                    Name = "United States",
                    States = new List<State>
                    {
                        new State {StateId = 1, Name = "Alabama"},
                        new State {StateId = 2, Name = "Alaska"},
                        new State {StateId = 3, Name = "Arkansas"},
                    },
                },
                new Country
                {
                    CountryId = 1,
                    Name = "Canada",
                    States = new List<State>
                    {
                        new State {StateId = 4, Name = "Alberta"},
                        new State {StateId = 5, Name = "British Columbia"},
                        new State {StateId = 6, Name = "Manitoba"},
                    },
                },
            };
        }
    }