This is a migrated thread and some comments may be shown as answers.

Navigating and Expanding Hierarchical Grid Using CommandItem.

1 Answer 27 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Princy
Top achievements
Rank 2
Princy asked on 10 Sep 2008, 02:17 PM

Requirements

RadControls version

2008.01.0415.20
.NET version

2.0
Visual Studio version

2005
programming language

C#
browser support

all browsers supported by RadControls


 
PROJECT DESCRIPTION

This project illustrates how to select and expand a hierarchical Grid using the navigation buttons in the CommandItem. This is achieved by adding a GridButtonColumn to select and expand the Grid. Hook the ItemCommand Event and perform the computation there. Below are the code snippets.

ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
        <div> 
            <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" PageSize="5" DataSourceID="SqlDataSource1" GridLines="None" OnItemCommand="RadGrid1_ItemCommand" Skin="Default2006" OnPreRender="RadGrid1_PreRender"
                <MasterTableView AutoGenerateColumns="False"  CommandItemDisplay="Top" DataSourceID="SqlDataSource1" > 
                     <CommandItemTemplate> 
                         <asp:LinkButton ID="LinkButton1" Text="Next"  CommandName="Next" runat="server"></asp:LinkButton> 
                         <asp:LinkButton ID="LinkButton2" Text="Previous"  CommandName="Prev"  runat="server"></asp:LinkButton> 
                     </CommandItemTemplate> 
                    <RowIndicatorColumn Visible="False"
                        <HeaderStyle Width="20px" /> 
                    </RowIndicatorColumn> 
                    <ExpandCollapseColumn Resizable="False"
                        <HeaderStyle Width="20px" /> 
                    </ExpandCollapseColumn> 
                    <Columns> 
                        <telerik:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" 
                            UniqueName="CustomerID"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" 
                            UniqueName="CompanyName"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" 
                            UniqueName="PostalCode"
                        </telerik:GridBoundColumn> 
                      <telerik:GridButtonColumn CommandName="Select" text="Select" HeaderText="SelectColumn" UniqueName="column" ></telerik:GridButtonColumn> 
                    </Columns> 
                    <EditFormSettings> 
                        <PopUpSettings ScrollBars="None" /> 
                    </EditFormSettings> 
                    <DetailTables> 
                     
                      <telerik:GridTableView runat="server" DataSourceID="SqlDataSource2"  > 
                        <Columns> 
                         <telerik:GridBoundColumn DataField="CustomerID" DataType="System.Int32" HeaderText="CustomerID" 
                            SortExpression="CustomerID" UniqueName="CustomerID"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" SortExpression="OrderID" 
                            UniqueName="OrderID"
                        </telerik:GridBoundColumn> 
                        </Columns> 
                          <RowIndicatorColumn Visible="False"
                              <HeaderStyle Width="20px" /> 
                          </RowIndicatorColumn> 
                          <ExpandCollapseColumn Resizable="False" Visible="False"
                              <HeaderStyle Width="20px" /> 
                          </ExpandCollapseColumn> 
                          <EditFormSettings> 
                              <PopUpSettings ScrollBars="None" /> 
                          </EditFormSettings> 
                      </telerik:GridTableView> 
                    </DetailTables> 
                </MasterTableView> 
                <ClientSettings> 
                 <Selecting AllowRowSelect="True" /> 
                </ClientSettings> 
            </telerik:RadGrid> 
            &nbsp;&nbsp; 
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString2 %>" 
                SelectCommand="SELECT [CustomerID], [CompanyName], [PostalCode], [Region] FROM [Customers]"
            </asp:SqlDataSource> 
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString2 %>" 
                SelectCommand="SELECT [OrderID], [CustomerID], [EmployeeID] FROM [Orders]"
            </asp:SqlDataSource> 
            <br /> 
        </div> 
    </form> 
</body> 
</html> 
 


C#:
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Data.Sql; 
using Telerik.Web.UI; 
 
public partial class _Default : System.Web.UI.Page  
    protected void Page_Load(object sender, EventArgs e) 
    { 
 
    } 
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) 
    { 
        if (e.CommandName == "Select") 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            item.Expanded = true
        } 
        if (e.CommandName == "Next") 
        { 
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
            { 
                if (item.Selected) 
                { 
                    item.Selected = false
                    item.Expanded = false
                    int CurrentIndex = item.ItemIndex; 
                    int NextIndex; 
                    if (CurrentIndex == RadGrid1.PageSize - 1) 
                    { 
                         NextIndex = 0
                    } 
                    else 
                    { 
                        NextIndex = CurrentIndex + 1; 
                    } 
                    RadGrid1.MasterTableView.Items[NextIndex].Selected = true
                    RadGrid1.MasterTableView.Items[NextIndex].Expanded = true
                    break; 
                } 
 
            } 
        } 
        else if (e.CommandName == "Prev") 
        { 
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
            { 
                if (item.Selected) 
                { 
                    item.Selected = false
                    item.Expanded = false
                    int CurrentIndex = item.ItemIndex; 
                    int PrevIndex; 
                    if (CurrentIndex == 0) 
                    { 
                        PrevIndex = RadGrid1.PageSize-1; 
                    } 
                    else 
                    { 
                        PrevIndex = CurrentIndex - 1; 
                    } 
                     
                    RadGrid1.MasterTableView.Items[PrevIndex].Selected = true
                    RadGrid1.MasterTableView.Items[PrevIndex].Expanded = true
                    break; 
                } 
 
            } 
        } 
    } 
    protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
        { 
            if (col.UniqueName == "ExpandColumn") 
            { 
                col.Display = false
            } 
        } 
        
    } 

1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 11 Sep 2008, 12:06 PM
Hi Princy,

I have transferred the code library entry to a forum post, so that other community members can benefit from it as well. I have also updated your Telerik points for your involvement.

Kind regards,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Princy
Top achievements
Rank 2
Answers by
Yavor
Telerik team
Share this question
or