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

[Solved] Two grids : can't get the same height for both of them !

7 Answers 201 Views
Grid
This is a migrated thread and some comments may be shown as answers.
RadTony
Top achievements
Rank 1
RadTony asked on 03 Feb 2010, 03:04 PM
I have two grids : the first grid is hierarchical and the second grid synchronizes its data with the first one.

The problem is : I can't get the same height for both of them. I tried a lot of things ... can you please help me ?

One thing I remarked just now : I added some rows in "Employees" table and a vertical scroll appears in grid2. I really don't understand how the height of this grid is predefined ... I wonder if it is ever possible to have the same height for both grids.

Here is the code, just change the connection to Northwind  database in "GetConnectionString()" method 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadGridSolutionTelerikLines.aspx.cs" Inherits="RadGridSolutionTelerikLines" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head> 
   <title></title
    <style type="text/css"
      .StaticColumn 
      {     
          font-family: Arial,Verdana, Helvetica; 
        font-size: 14px; 
        font-weight: normal;    
          border: none; 
          position: relative; 
      }  
  
        .RadGrid td{padding:0}  
         
       .rgExpand   
        {   
          background:url('Images/CollapsedButton.gif') center no-repeat !important;        
        }   
       
       .rgCollapse   
        {   
          background:url('Images/ExpandedButton.gif') center no-repeat !important;   
        }  
         
.RadGrid_Vista tr.DetailRow 
    background:#ebe4d6; 
 
.DetailTable_Vista 
    background:#f4ede1; 
 
.RadGrid_Vista .DetailTable_Vista .GridHeader_Vista, 
.RadGrid_Vista .DetailTable_Vista .ResizeHeader_Vista 
    padding-top:2px; 
    padding-bottom:2px; 
    background-position:0 -6px; 
}         
    </style>      
</head> 
<body> 
    <form runat="server" id="mainForm" method="post">         
      <telerik:RadScriptManager ID="RadScriptManager1" runat="server" /> 
        <!-- content start --> 
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"
            <AjaxSettings> 
                <telerik:AjaxSetting AjaxControlID="RadGrid1"
                    <UpdatedControls> 
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />   
                        <telerik:AjaxUpdatedControl ControlID="RadGrid2" />                       
                    </UpdatedControls> 
                </telerik:AjaxSetting> 
            </AjaxSettings> 
        </telerik:RadAjaxManager> 
        <!-- DataSourceID="SqlDataSource1" --> 
        <table border="0" cellspacing="0" cellpadding="0" style="height:100%;">  
        <tr> 
        <td style="vertical-align:top;height:100%">         
        <telerik:RadGrid ID="RadGrid1" Skin="Vista" OnPreRender="RadGrid1_PreRender"  
                runat="server" DataSourceID="SqlDataSource1"  OnColumnCreated="RadGrid1_ColumnCreated" 
            OnItemCreated="RadGrid1_ItemCreated" OnItemDataBound="RadGrid1_ItemDataBound"  
                Width="100%" onselectedindexchanged="RadGrid1_SelectedIndexChanged"
            <MasterTableView  HierarchyDefaultExpanded="false" EnableNoRecordsTemplate="false" HierarchyLoadMode="ServerBind" AllowSorting="true" Width="800px" 
                DataKeyNames="EmployeeID" ExpandCollapseColumn-ButtonType="ImageButton" ExpandCollapseColumn-Display="true" 
                ExpandCollapseColumn-ExpandImageUrl="Images/CollapsedButton.gif" ExpandCollapseColumn-CollapseImageUrl="Images/ExpandedButton.gif"
                <ItemStyle Wrap="false" Width="200px" BackColor="Azure" Height="17px" BorderWidth="1"/> 
                <AlternatingItemStyle Wrap="false" BackColor="BlanchedAlmond" Width="200px" Height="17px" BorderWidth="1"/> 
                <HeaderStyle Wrap="false" Width="1500px" Height="20px"/> 
                 
                <SelfHierarchySettings ParentKeyName="ReportsTo" KeyName="EmployeeID"/>                 
                 
            </MasterTableView> 
             
            <ClientSettings AllowExpandCollapse="true" EnablePostBackOnRowClick="true" EnableRowHoverStyle="true">       
            <Selecting AllowRowSelect="true" />       
            </ClientSettings> 
        </telerik:RadGrid> 
        </td> 
        <td class="StaticColumn" style="vertical-align:top;height:auto;">  
        <telerik:RadGrid ID="RadGrid2" ShowStatusBar="true" EnableViewState="true" 
            runat="server" AutoGenerateColumns="true" AllowSorting="False" AllowMultiRowSelection="False" Width="800px" GridLines="None"
            <MasterTableView ItemStyle-Wrap="false" Height="20px">       
            <HeaderStyle Wrap="false" Height="20px"/>             
            <AlternatingItemStyle Height="17px"  BackColor="BlanchedAlmond" Wrap="false" />  
            <ItemStyle Height="17px"  BackColor="Azure" Wrap="false" />             
                <Columns> 
                    <telerik:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" HeaderButtonType="TextButton" 
                        DataField="EmployeeID" UniqueName="EmployeeID" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="LastName" HeaderText="LastName" HeaderButtonType="TextButton" 
                        DataField="LastName" UniqueName="LastName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="FirstName" HeaderText="FirstName" HeaderButtonType="TextButton" 
                        DataField="FirstName" UniqueName="FirstName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="Title" HeaderText="Title" HeaderButtonType="TextButton" 
                        DataField="Title" UniqueName="Title" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="ReportsTo" HeaderText="ReportsTo" HeaderButtonType="TextButton" 
                        DataField="ReportsTo" UniqueName="ReportsTo" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" HeaderButtonType="TextButton" 
                        DataField="EmployeeID" UniqueName="EmployeeID" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="LastName" HeaderText="LastName" HeaderButtonType="TextButton" 
                        DataField="LastName" UniqueName="LastName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="FirstName" HeaderText="FirstName" HeaderButtonType="TextButton" 
                        DataField="FirstName" UniqueName="FirstName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="Title" HeaderText="Title" HeaderButtonType="TextButton" 
                        DataField="Title" UniqueName="Title" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="ReportsTo" HeaderText="ReportsTo" HeaderButtonType="TextButton" 
                        DataField="ReportsTo" UniqueName="ReportsTo" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn>      
                    <telerik:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" HeaderButtonType="TextButton" 
                        DataField="EmployeeID" UniqueName="EmployeeID" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="LastName" HeaderText="LastName" HeaderButtonType="TextButton" 
                        DataField="LastName" UniqueName="LastName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="FirstName" HeaderText="FirstName" HeaderButtonType="TextButton" 
                        DataField="FirstName" UniqueName="FirstName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="Title" HeaderText="Title" HeaderButtonType="TextButton" 
                        DataField="Title" UniqueName="Title" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="ReportsTo" HeaderText="ReportsTo" HeaderButtonType="TextButton" 
                        DataField="ReportsTo" UniqueName="ReportsTo" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn>  
                    <telerik:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" HeaderButtonType="TextButton" 
                        DataField="EmployeeID" UniqueName="EmployeeID" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="LastName" HeaderText="LastName" HeaderButtonType="TextButton" 
                        DataField="LastName" UniqueName="LastName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="FirstName" HeaderText="FirstName" HeaderButtonType="TextButton" 
                        DataField="FirstName" UniqueName="FirstName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="Title" HeaderText="Title" HeaderButtonType="TextButton" 
                        DataField="Title" UniqueName="Title" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="ReportsTo" HeaderText="ReportsTo" HeaderButtonType="TextButton" 
                        DataField="ReportsTo" UniqueName="ReportsTo" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                    </telerik:GridBoundColumn>                                                         
                </Columns> 
            </MasterTableView> 
            <clientsettings allowexpandcollapse="false" EnableRowHoverStyle="true">                 
                    <Scrolling AllowScroll="true" UseStaticHeaders="True"></Scrolling> 
            </clientsettings> 
             
        </telerik:RadGrid> 
        </td> 
        </tr> 
        </table>         
        <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
            ProviderName="System.Data.SqlClient" SelectCommand="SELECT EmployeeID, LastName, FirstName, Title, ReportsTo FROM Employees" 
            runat="server"></asp:SqlDataSource> 
        <!-- content end --> 
    </form> 
</body> 
</html> 
 
 
 
 
 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
using System.Reflection; 
using System.Data; 
using System.Data.SqlClient;  
 
public partial class RadGridSolutionTelerikLines : System.Web.UI.Page 
 
    public void Page_Load(object sender, EventArgs e) 
    { 
        bindGrid2(); 
        
        if (Assembly.GetAssembly(typeof(ScriptManager)).FullName.IndexOf("3.5") != -1) 
        { 
            RadGrid1.MasterTableView.FilterExpression = @"it[""ReportsTo""] = Convert.DBNull"; 
        } 
        else 
        { 
            RadGrid1.MasterTableView.FilterExpression = "ReportsTo IS NULL"
        } 
 
        if (RadGrid1.MasterTableView.DetailTables != null) 
        { 
            RadGrid1.MasterTableView.DetailTables[0].AlternatingItemStyle.BackColor = System.Drawing.Color.Azure  ; 
            RadGrid1.MasterTableView.DetailTables[0].ItemStyle.BackColor = System.Drawing.Color.BlanchedAlmond; 
 
            if (RadGrid1.MasterTableView.DetailTables[0].DetailTables[0] != null) 
            { 
                RadGrid1.MasterTableView.DetailTables[0].DetailTables[0].AlternatingItemStyle.BackColor = System.Drawing.Color.Azure; 
                RadGrid1.MasterTableView.DetailTables[0].DetailTables[0].ItemStyle.BackColor = System.Drawing.Color.BlanchedAlmond; 
            } 
        } 
 
    } 
 
    public void Page_PreRenderComplete(object sender, EventArgs e) 
    { 
        HideExpandColumnRecursive(RadGrid1.MasterTableView); 
 
        string ActualNodes = string.Empty; 
 
        foreach (GridDataItem dataItem in RadGrid1.Items) 
        { 
            if (dataItem.Visible) 
            { 
                ActualNodes += dataItem["EmployeeID"].Text + ","; 
            } 
        } 
 
 
        if (ActualNodes.Length > 0) 
            ActualNodesActualNodes = ActualNodes.Substring(0, ActualNodes.Length - 1); 
 
        RadGrid2.Controls.Clear(); 
        RadGrid2.DataSource = getDetails(ActualNodes); 
        RadGrid2.DataBind(); 
 
        if (Session["RadGrid1_SelectedValue"] != null) 
        { 
            for (int m = 0; m < RadGrid2.Items.Count; m++) 
            { 
                if (RadGrid2.Items[m]["EmployeeID"].Text.Equals(Session["RadGrid1_SelectedValue"].ToString())) 
                { 
                    RadGrid2.Items[m].Selected = true
                } 
            } 
        } 
 
 
    } 
 
    public void HideExpandColumnRecursive(GridTableView tableView) 
    { 
        GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView); 
        foreach (GridNestedViewItem nestedViewItem in nestedViewItems) 
        { 
            foreach (GridTableView nestedView in nestedViewItem.NestedTableViews) 
            { 
                nestedView.Style["border"] = "0"; 
 
                Button MyExpandCollapseButton = (Button)nestedView.ParentItem.FindControl("MyExpandCollapseButton"); 
                if (nestedView.Items.Count == 0) 
                { 
                    if (MyExpandCollapseButton != null) 
                    { 
                        MyExpandCollapseButton.Style["visibility"] = "hidden"; 
                    } 
                    nestedViewItem.Visible = false
                } 
                else 
                { 
                    if (MyExpandCollapseButton != null) 
                    { 
                        MyExpandCollapseButton.Style.Remove("visibility"); 
                    } 
                } 
 
                if (nestedView.HasDetailTables) 
                { 
                    HideExpandColumnRecursive(nestedView); 
                } 
            } 
        } 
    } 
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        CreateExpandCollapseButton(e.Item, "EmployeeID"); 
 
        if (e.Item is GridHeaderItem && e.Item.OwnerTableView != RadGrid1.MasterTableView) 
        { 
            e.Item.Style["display"] = "none"; 
        } 
 
        if (e.Item is GridNestedViewItem) 
        { 
            e.Item.Cells[0].Visible = false
 
            Table table = (Table)e.Item.OwnerTableView.Controls[0]; 
 
            GridItem prevItem = (GridItem)table.Rows[table.Rows.Count - 2]; 
            // Why -3? We need to skip the NestedViewItem and the preceding DataItem's invisible EditItem 
 
            if (prevItem.ItemType == GridItemType.AlternatingItem) 
            { 
                e.Item.CssClass = "DetailAltRow"
            } 
            else 
            { 
                e.Item.CssClass = "DetailRow"
            } 
            /* 
            e.Item.Cells[0].CssClass = "DetailRowCell"
            e.Item.Cells[1].CssClass = "DetailRowCell"
             */ 
        } 
 
    
    } 
 
    protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e) 
    { 
        if (e.Column is GridExpandColumn) 
        { 
            e.Column.Visible = false
        } 
        else if (e.Column is GridBoundColumn) 
        { 
            e.Column.HeaderStyle.Width = Unit.Pixel(100); 
        } 
    } 
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        CreateExpandCollapseButton(e.Item, "EmployeeID"); 
    } 
 
    public void CreateExpandCollapseButton(GridItem item, string columnUniqueName) 
    { 
        if (item is GridDataItem) 
        { 
            if (item.FindControl("MyExpandCollapseButton") == null) 
            { 
                Button button = new Button(); 
                button.Click += new EventHandler(button_Click); 
                button.CommandName = "ExpandCollapse"
                button.CssClass = (item.Expanded) ? "rgCollapse" : "rgExpand"; 
                button.ID = "MyExpandCollapseButton"
 
                if (item.OwnerTableView.HierarchyLoadMode == GridChildLoadMode.Client) 
                { 
                    string script = String.Format(@"$find(""{0}"")._toggleExpand(this, event); return false;", item.Parent.Parent.ClientID); 
 
                    button.OnClientClick = script
                } 
 
                int level = item.ItemIndexHierarchical.Split(':').Length; 
                if (level > 1) 
                { 
                    button.Style["margin-left"] = level + 10 + "px"; 
                    button.Style["height"] = "15px"; 
                } 
 
 
                TableCell cell = ((GridDataItem)item)[columnUniqueName]; 
                cell.Controls.Add(button); 
 
                LiteralControl liSpace = new LiteralControl("&nbsp;"); 
                cell.Controls.Add(liSpace); 
 
                cell.Controls.Add(new LiteralControl(((GridDataItem)item).GetDataKeyValue(columnUniqueName).ToString())); 
                cell.Style["height"] = "15px"; 
 
            } 
        } 
    } 
 
    protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
 
        string ActualNodes = string.Empty; 
 
        foreach (GridDataItem dataItem in RadGrid1.Items) 
        { 
            if (dataItem.Visible) 
            { 
                ActualNodes += dataItem["EmployeeID"].Text + ","; 
            } 
        } 
 
 
        if (ActualNodes.Length > 0) 
            ActualNodesActualNodes = ActualNodes.Substring(0, ActualNodes.Length - 1); 
 
        RadGrid2.Controls.Clear(); 
        RadGrid2.DataSource = getDetails(ActualNodes); 
        RadGrid2.DataBind(); 
        //Response.Write("<script> alert(" + sTest + ");</script>");  
    } 
 
 
    void button_Click(object sender, EventArgs e) 
    { 
        ((Button)sender).CssClass = (((Button)sender).CssClass == "rgExpand") ? "rgCollapse" : "rgExpand"; 
    } 
 
 
    private void bindGrid2() 
    { 
 
        RadGrid2.Controls.Clear(); 
        string initialNodes = string.Empty; 
        for (int m = 0; m < RadGrid1.Items.Count; m++) 
        { 
            if (!RadGrid1.Items[m]["EmployeeID"].Text.ToLower().Equals("&nbsp;") && RadGrid1.Items[m].Visible) 
                initialNodes += RadGrid1.Items[m]["EmployeeID"].Text + ","; 
        } 
 
        if (initialNodes.Length > 0) 
        { 
            RadGrid2.DataSource = getEmployees(initialNodes.Substring(0, initialNodes.Length - 1)); 
        } 
        else 
            RadGrid2.DataSource = getEmployees(""); 
 
        RadGrid2.DataBind(); 
    } 
 
    private DataView getEmployees(string initialNodes) 
    { 
        DataSet ds = new DataSet(); 
 
        string sqlFinal = string.Empty; 
        if (!initialNodes.Equals(string.Empty)) 
        { 
            sqlFinal = "Where EmployeeID in (" + initialNodes + ")"; 
        } 
 
        using (SqlConnection conn = new SqlConnection()) 
        { 
            conn.ConnectionString = GetConnectionString(); 
            SqlCommand cmd = new SqlCommand("SELECT [EmployeeID], [LastName], [FirstName], [Title], [ReportsTo] FROM [Employees] " + sqlFinal, conn); 
            conn.Open(); 
 
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd); 
 
            sqlAdapter.Fill(ds); 
        } 
 
        DataView dv = new DataView(ds.Tables[0]); 
        return dv; 
    } 
 
    private DataTable getChildNodesByID(string ParentID) 
    { 
        DataSet ds = new DataSet(); 
        string sql = "SELECT [EmployeeID], [LastName], [FirstName], [Title], [ReportsTo] FROM [Employees] Where ReportsTo = " + ParentID; 
 
        using (SqlConnection conn = new SqlConnection()) 
        { 
            conn.ConnectionString = GetConnectionString(); 
            SqlCommand cmd = new SqlCommand(sql, conn); 
            conn.Open(); 
 
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd); 
            sqlAdapter.Fill(ds); 
        } 
 
        DataTable dt = new DataTable(); 
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 
            return ds.Tables[0]; 
        else 
            return null; 
    } 
 
    private DataView getDetails(string ActualNodes) 
    { 
        DataSet ds = new DataSet(); 
 
 
        //string noeuds = ActualNodes.Substring(0, ActualNodes.Length - 1);  
 
        string[] tabNoeuds = ActualNodes.Split(','); 
        string swhen = " CASE "
 
        for (int i = 0; i < tabNoeuds.Length; i++) 
        { 
            swhen += " WHEN EmployeeID = " + tabNoeuds[i] + " THEN " + (i + 1) + " "; 
        } 
        swhen += " END "; 
 
        string sql = "SELECT [EmployeeID], [LastName], [FirstName], [Title], [ReportsTo] FROM [Employees] Where EmployeeID in (" + ActualNodes + ")"; 
        sql += " ORDER BY " + swhen; 
        using (SqlConnection conn = new SqlConnection()) 
        { 
            conn.ConnectionString = GetConnectionString(); 
            SqlCommand cmd = new SqlCommand(sql, conn); 
            conn.Open(); 
 
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd); 
            sqlAdapter.Fill(ds); 
        } 
 
        DataView dv = new DataView(ds.Tables[0]); 
        return dv; 
    } 
 
 
 
    static private string GetConnectionString() 
    { 
        return "server=STATION00374;database=Northwind;Integrated Security=SSPI;"; 
    } 
 
 
    protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
       if (Session["RadGrid1_SelectedValue"] != null) 
            Session.Remove("RadGrid1_SelectedValue"); 
 
        Session["RadGrid1_SelectedValue"] = RadGrid1.SelectedValue.ToString();  
    } 
 

7 Answers, 1 is accepted

Sort by
0
RadTony
Top achievements
Rank 1
answered on 04 Feb 2010, 10:24 AM
I still can't find a solution to have the same height for both grids ...
With DevXpress, I had no such issue (see attachment).

Has someone at least an idea where to search for solving this strange problem ?
How to get a Grid which resizes itself according to the number of rows it contains ? (see attachments)

Isn't it strange to have a 500 px height grid, when there is only one row and unnecessary vertical scroll when there are more rows ?
I'll try to put a rowspan in my <td> just to see ...
0
Dimo
Telerik team
answered on 04 Feb 2010, 12:35 PM
Hello RadTony (nice nickname :) )

I suppose the following help article will throw some light on your case:

http://www.telerik.com/help/aspnet-ajax/height-vs-scrollheight.html

Generally, in order to have a predictable RadGrid height, you should always use scrolling (with or without static headers). Otherwise the control is as high as the sum of all its items.

Best wishes,
Dimo
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
RadTony
Top achievements
Rank 1
answered on 04 Feb 2010, 01:48 PM
Thanks Dimo for your reply !

I found that if I put the Allowscroll attribute to false as this :
<Scrolling AllowScroll="false" ScrollHeight="1000px" UseStaticHeaders="true"></Scrolling>
then, RadGrid2's height is synchronized with RadGrid1's ... 

And then as I could no more scroll RadGrid2, I just put it in a DIV with a style defined like this to enable scrolling :
<div style="width:800px;overflow:scroll; vertical-align:top;">
 Radgrid2
</div>

Finally, the problem is solved (see the wonderful result in attachment). I'll post the code below. 
Now, the next problem is how to enable editing all rows in RadGrid2 and not just the parent row ... 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadGridMarche1.aspx.cs" Inherits="RadGridMarche1" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head> 
   <title></title
    <style type="text/css"
      .StaticColumn 
      {     
          font-family: Arial,Verdana, Helvetica; 
        font-size: 14px; 
        font-weight: normal;    
          border: none; 
          position: relative; 
      }  
  
        .RadGrid td{padding:0}  
         
       .rgExpand   
        {   
          background:url('Images/CollapsedButton.gif') center no-repeat !important;        
        }   
       
       .rgCollapse   
        {   
          background:url('Images/ExpandedButton.gif') center no-repeat !important;   
        }  
         
.RadGrid_Vista tr.DetailRow 
    background:#ebe4d6; 
 
.DetailTable_Vista 
    background:#f4ede1; 
 
.RadGrid_Vista .DetailTable_Vista .GridHeader_Vista, 
.RadGrid_Vista .DetailTable_Vista .ResizeHeader_Vista 
    padding-top:2px; 
    padding-bottom:2px; 
    background-position:0 -6px; 
}         
    </style>      
</head> 
<body> 
    <form runat="server" id="mainForm" method="post">         
      <telerik:RadScriptManager ID="RadScriptManager1" runat="server" /> 
        <!-- content start --> 
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"
            <AjaxSettings> 
                <telerik:AjaxSetting AjaxControlID="RadGrid1"
                    <UpdatedControls> 
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />   
                        <telerik:AjaxUpdatedControl ControlID="RadGrid2" />                       
                    </UpdatedControls> 
                </telerik:AjaxSetting> 
            </AjaxSettings> 
        </telerik:RadAjaxManager> 
        <!-- DataSourceID="SqlDataSource1" --> 
        <br /> 
            <asp:Label ID="LblRadgrid1_height" runat="server" Width="900px" BackColor="red"></asp:Label> 
        <br /> 
        <table border="0" cellspacing="0" cellpadding="0" style="height:auto;">  
        <tr> 
        <td style="vertical-align:top;height:auto;">         
        <telerik:RadGrid ID="RadGrid1" Skin="Vista" OnPreRender="RadGrid1_PreRender"  
                runat="server" DataSourceID="SqlDataSource1"  OnColumnCreated="RadGrid1_ColumnCreated" 
            OnItemCreated="RadGrid1_ItemCreated" OnItemDataBound="RadGrid1_ItemDataBound"  
                Width="100%" onselectedindexchanged="RadGrid1_SelectedIndexChanged"
            <MasterTableView  HierarchyDefaultExpanded="false" EnableNoRecordsTemplate="false" HierarchyLoadMode="ServerBind" AllowSorting="true" Width="800px" 
                DataKeyNames="EmployeeID" ExpandCollapseColumn-ButtonType="ImageButton" ExpandCollapseColumn-Display="true" 
                ExpandCollapseColumn-ExpandImageUrl="Images/CollapsedButton.gif" ExpandCollapseColumn-CollapseImageUrl="Images/ExpandedButton.gif"
                <ItemStyle Wrap="false" Width="200px" BackColor="Azure" Height="17px" BorderWidth="1"/> 
                <AlternatingItemStyle Wrap="false" BackColor="BlanchedAlmond" Width="200px" Height="17px" BorderWidth="1"/> 
                <HeaderStyle Wrap="false" Width="1500px" Height="20px"/> 
                 
                <SelfHierarchySettings ParentKeyName="ReportsTo" KeyName="EmployeeID"/>                 
                 
            </MasterTableView> 
             
            <ClientSettings AllowExpandCollapse="true" EnablePostBackOnRowClick="true" EnableRowHoverStyle="true">       
            <Selecting AllowRowSelect="true" />       
            </ClientSettings> 
        </telerik:RadGrid> 
        </td> 
        <td style="vertical-align:top;height:100%;">  
            <table style="vertical-align:top" cellpadding="0" cellspacing="0">  
                <tr valign="top"
                   <td valign="top">  
                        <div style="width:800px;overflow:scroll; vertical-align:top;"
                            <telerik:RadGrid ID="RadGrid2" ShowStatusBar="true" EnableViewState="true" 
                runat="server" AutoGenerateColumns="true" AllowSorting="False" AllowMultiRowSelection="False" Width="2200px" GridLines="None"
                <MasterTableView ItemStyle-Wrap="false">                   
                <HeaderStyle Wrap="false" Height="20px"/>             
                <AlternatingItemStyle Height="17px"  BackColor="BlanchedAlmond" Wrap="false" />  
                <ItemStyle Height="17px"  BackColor="Azure" Wrap="false" />             
                    <Columns> 
                        <telerik:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" HeaderButtonType="TextButton" 
                            DataField="EmployeeID" UniqueName="EmployeeID" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="LastName" HeaderText="LastName" HeaderButtonType="TextButton" 
                            DataField="LastName" UniqueName="LastName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="FirstName" HeaderText="FirstName" HeaderButtonType="TextButton" 
                            DataField="FirstName" UniqueName="FirstName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="Title" HeaderText="Title" HeaderButtonType="TextButton" 
                            DataField="Title" UniqueName="Title" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="ReportsTo" HeaderText="ReportsTo" HeaderButtonType="TextButton" 
                            DataField="ReportsTo" UniqueName="ReportsTo" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn>  
                        <telerik:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" HeaderButtonType="TextButton" 
                            DataField="EmployeeID" UniqueName="EmployeeID" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="LastName" HeaderText="LastName" HeaderButtonType="TextButton" 
                            DataField="LastName" UniqueName="LastName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="FirstName" HeaderText="FirstName" HeaderButtonType="TextButton" 
                            DataField="FirstName" UniqueName="FirstName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="Title" HeaderText="Title" HeaderButtonType="TextButton" 
                            DataField="Title" UniqueName="Title" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="ReportsTo" HeaderText="ReportsTo" HeaderButtonType="TextButton" 
                            DataField="ReportsTo" UniqueName="ReportsTo" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn>      
                        <telerik:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" HeaderButtonType="TextButton" 
                            DataField="EmployeeID" UniqueName="EmployeeID" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="LastName" HeaderText="LastName" HeaderButtonType="TextButton" 
                            DataField="LastName" UniqueName="LastName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="FirstName" HeaderText="FirstName" HeaderButtonType="TextButton" 
                            DataField="FirstName" UniqueName="FirstName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="Title" HeaderText="Title" HeaderButtonType="TextButton" 
                            DataField="Title" UniqueName="Title" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="ReportsTo" HeaderText="ReportsTo" HeaderButtonType="TextButton" 
                            DataField="ReportsTo" UniqueName="ReportsTo" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn>  
                        <telerik:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" HeaderButtonType="TextButton" 
                            DataField="EmployeeID" UniqueName="EmployeeID" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="LastName" HeaderText="LastName" HeaderButtonType="TextButton" 
                            DataField="LastName" UniqueName="LastName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="FirstName" HeaderText="FirstName" HeaderButtonType="TextButton" 
                            DataField="FirstName" UniqueName="FirstName" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="Title" HeaderText="Title" HeaderButtonType="TextButton" 
                            DataField="Title" UniqueName="Title" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="ReportsTo" HeaderText="ReportsTo" HeaderButtonType="TextButton" 
                            DataField="ReportsTo" UniqueName="ReportsTo" ItemStyle-Wrap="false" ItemStyle-Width="200px" ItemStyle-Height="17px"
                        </telerik:GridBoundColumn>   
                        <telerik:GridEditCommandColumn Display="true" EditImageUrl="Images/ExpandedButton.gif" EditText="edit" AutoPostBackOnFilter="true"></telerik:GridEditCommandColumn>                                                       
                    </Columns> 
                </MasterTableView> 
                <clientsettings allowexpandcollapse="false" EnableRowHoverStyle="true">                 
                        <Scrolling AllowScroll="false" UseStaticHeaders="true"></Scrolling>   
                                                                                  
                </clientsettings>      
                        
            </telerik:RadGrid> 
                        </div> 
                    </td> 
                </tr> 
            </table> 
        </td> 
         
        </tr>         
        </table>         
        <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
            ProviderName="System.Data.SqlClient" SelectCommand="SELECT EmployeeID, LastName, FirstName, Title, ReportsTo FROM Employees" 
            runat="server"></asp:SqlDataSource> 
        <!-- content end --> 
    </form> 
</body> 
</html> 
 
 
-------------------------------- 
 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
using System.Reflection; 
using System.Data; 
using System.Data.SqlClient; 
 
public partial class RadGridMarche1 : System.Web.UI.Page 
 
    public void Page_Load(object sender, EventArgs e) 
    { 
        bindGrid2(); 
        
        if (Assembly.GetAssembly(typeof(ScriptManager)).FullName.IndexOf("3.5") != -1) 
        { 
            RadGrid1.MasterTableView.FilterExpression = @"it[""ReportsTo""] = Convert.DBNull"; 
        } 
        else 
        { 
            RadGrid1.MasterTableView.FilterExpression = "ReportsTo IS NULL"
        } 
         
        if (RadGrid1.MasterTableView.DetailTables != null) 
        { 
            RadGrid1.MasterTableView.DetailTables[0].AlternatingItemStyle.BackColor = System.Drawing.Color.Azure  ; 
            RadGrid1.MasterTableView.DetailTables[0].ItemStyle.BackColor = System.Drawing.Color.BlanchedAlmond; 
 
            if (RadGrid1.MasterTableView.DetailTables[0].DetailTables[0] != null) 
            { 
                RadGrid1.MasterTableView.DetailTables[0].DetailTables[0].AlternatingItemStyle.BackColor = System.Drawing.Color.Azure; 
                RadGrid1.MasterTableView.DetailTables[0].DetailTables[0].ItemStyle.BackColor = System.Drawing.Color.BlanchedAlmond; 
            } 
        } 
         
 
    } 
 
    public void Page_PreRenderComplete(object sender, EventArgs e) 
    { 
        HideExpandColumnRecursive(RadGrid1.MasterTableView); 
 
        string ActualNodes = string.Empty; 
 
        int counterVisibleNodes = 0
        foreach (GridDataItem dataItem in RadGrid1.Items) 
        { 
            if (dataItem.Visible) 
            { 
                ActualNodes += dataItem["EmployeeID"].Text + ","; 
                counterVisibleNodes++; 
            } 
        } 
 
 
        if (ActualNodes.Length > 0) 
            ActualNodesActualNodes = ActualNodes.Substring(0, ActualNodes.Length - 1); 
         
        RadGrid2.Controls.Clear(); 
        RadGrid2.DataSource = getDetails(ActualNodes); 
        RadGrid2.DataBind(); 
         
        if (Session["RadGrid1_SelectedValue"] != null) 
        { 
            for (int m = 0; m < RadGrid2.Items.Count; m++) 
            { 
                if (RadGrid2.Items[m]["EmployeeID"].Text.Equals(Session["RadGrid1_SelectedValue"].ToString())) 
                { 
                    RadGrid2.Items[m].Selected = true
                    break; 
                } 
            } 
            Session.Remove("RadGrid1_SelectedValue"); 
        } 
 
 
    } 
 
    public void HideExpandColumnRecursive(GridTableView tableView) 
    { 
        GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView); 
        foreach (GridNestedViewItem nestedViewItem in nestedViewItems) 
        { 
            foreach (GridTableView nestedView in nestedViewItem.NestedTableViews) 
            { 
                nestedView.Style["border"] = "0"; 
 
                Button MyExpandCollapseButton = (Button)nestedView.ParentItem.FindControl("MyExpandCollapseButton"); 
                if (nestedView.Items.Count == 0) 
                { 
                    if (MyExpandCollapseButton != null) 
                    { 
                        MyExpandCollapseButton.Style["visibility"] = "hidden"; 
                    } 
                    nestedViewItem.Visible = false
                } 
                else 
                { 
                    if (MyExpandCollapseButton != null) 
                    { 
                        MyExpandCollapseButton.Style.Remove("visibility"); 
                    } 
                } 
 
                if (nestedView.HasDetailTables) 
                { 
                    HideExpandColumnRecursive(nestedView); 
                } 
            } 
        } 
    } 
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        CreateExpandCollapseButton(e.Item, "EmployeeID"); 
 
        if (e.Item is GridHeaderItem && e.Item.OwnerTableView != RadGrid1.MasterTableView) 
        { 
            e.Item.Style["display"] = "none"; 
        } 
 
        if (e.Item is GridNestedViewItem) 
        { 
            e.Item.Cells[0].Visible = false
 
            Table table = (Table)e.Item.OwnerTableView.Controls[0]; 
 
            GridItem prevItem = (GridItem)table.Rows[table.Rows.Count - 1]; 
            // Why -3? We need to skip the NestedViewItem and the preceding DataItem's invisible EditItem 
 
            if (prevItem.ItemType == GridItemType.AlternatingItem) 
            { 
                e.Item.CssClass = "DetailAltRow"
            } 
            else 
            { 
                e.Item.CssClass = "DetailRow"
            } 
            /* 
            e.Item.Cells[0].CssClass = "DetailRowCell"
            e.Item.Cells[1].CssClass = "DetailRowCell"
             */ 
        } 
 
    
    } 
 
    protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e) 
    { 
        if (e.Column is GridExpandColumn) 
        { 
            e.Column.Visible = false
        } 
        else if (e.Column is GridBoundColumn) 
        { 
            e.Column.HeaderStyle.Width = Unit.Pixel(100); 
        } 
    } 
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        CreateExpandCollapseButton(e.Item, "EmployeeID"); 
    } 
 
    public void CreateExpandCollapseButton(GridItem item, string columnUniqueName) 
    { 
        if (item is GridDataItem) 
        { 
            if (item.FindControl("MyExpandCollapseButton") == null) 
            { 
                Button button = new Button(); 
                button.Click += new EventHandler(button_Click); 
                button.CommandName = "ExpandCollapse"
                button.CssClass = (item.Expanded) ? "rgCollapse" : "rgExpand"; 
                button.ID = "MyExpandCollapseButton"
 
                if (item.OwnerTableView.HierarchyLoadMode == GridChildLoadMode.Client) 
                { 
                    string script = String.Format(@"$find(""{0}"")._toggleExpand(this, event); return false;", item.Parent.Parent.ClientID); 
 
                    button.OnClientClick = script
                } 
 
                int level = item.ItemIndexHierarchical.Split(':').Length; 
                if (level > 1) 
                { 
                    button.Style["margin-left"] = level + 10 + "px"; 
                    button.Style["height"] = "15px"; 
                } 
 
 
                TableCell cell = ((GridDataItem)item)[columnUniqueName]; 
                cell.Controls.Add(button); 
 
                LiteralControl liSpace = new LiteralControl("&nbsp;"); 
                cell.Controls.Add(liSpace); 
 
                cell.Controls.Add(new LiteralControl(((GridDataItem)item).GetDataKeyValue(columnUniqueName).ToString())); 
                cell.Style["height"] = "15px"; 
 
            } 
        } 
    } 
 
    protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
 
        string ActualNodes = string.Empty; 
 
        foreach (GridDataItem dataItem in RadGrid1.Items) 
        { 
            if (dataItem.Visible) 
            { 
                ActualNodes += dataItem["EmployeeID"].Text + ","; 
            } 
        } 
 
 
        if (ActualNodes.Length > 0) 
            ActualNodesActualNodes = ActualNodes.Substring(0, ActualNodes.Length - 1); 
 
        RadGrid2.Controls.Clear(); 
        RadGrid2.DataSource = getDetails(ActualNodes); 
        RadGrid2.DataBind(); 
        //Response.Write("<script> alert(" + sTest + ");</script>");  
    } 
 
 
    void button_Click(object sender, EventArgs e) 
    { 
        ((Button)sender).CssClass = (((Button)sender).CssClass == "rgExpand") ? "rgCollapse" : "rgExpand"; 
        LblRadgrid1_height.Text = RadGrid1.Height.Value.ToString(); 
        //Response.Write("<script language='javascript'> document.alert(" + RadGrid1.Height.Value.ToString() + ");</script>"); 
    } 
 
 
    private void bindGrid2() 
    { 
 
        RadGrid2.Controls.Clear(); 
        string initialNodes = string.Empty; 
        for (int m = 0; m < RadGrid1.Items.Count; m++) 
        { 
            if (!RadGrid1.Items[m]["EmployeeID"].Text.ToLower().Equals("&nbsp;") && RadGrid1.Items[m].Visible) 
                initialNodes += RadGrid1.Items[m]["EmployeeID"].Text + ","; 
        } 
 
        if (initialNodes.Length > 0) 
        { 
            RadGrid2.DataSource = getEmployees(initialNodes.Substring(0, initialNodes.Length - 1)); 
        } 
        else 
            RadGrid2.DataSource = getEmployees(""); 
 
        RadGrid2.DataBind(); 
    } 
 
    private DataView getEmployees(string initialNodes) 
    { 
        DataSet ds = new DataSet(); 
 
        string sqlFinal = string.Empty; 
        if (!initialNodes.Equals(string.Empty)) 
        { 
            sqlFinal = "Where EmployeeID in (" + initialNodes + ")"; 
        } 
 
        using (SqlConnection conn = new SqlConnection()) 
        { 
            conn.ConnectionString = GetConnectionString(); 
            SqlCommand cmd = new SqlCommand("SELECT top 1 [EmployeeID], [LastName], [FirstName], [Title], [ReportsTo] FROM [Employees] " + sqlFinal, conn); 
            conn.Open(); 
 
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd); 
 
            sqlAdapter.Fill(ds); 
        } 
 
        DataView dv = new DataView(ds.Tables[0]); 
        return dv; 
    } 
 
    private DataTable getChildNodesByID(string ParentID) 
    { 
        DataSet ds = new DataSet(); 
        string sql = "SELECT [EmployeeID], [LastName], [FirstName], [Title], [ReportsTo] FROM [Employees] Where ReportsTo = " + ParentID; 
 
        using (SqlConnection conn = new SqlConnection()) 
        { 
            conn.ConnectionString = GetConnectionString(); 
            SqlCommand cmd = new SqlCommand(sql, conn); 
            conn.Open(); 
 
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd); 
            sqlAdapter.Fill(ds); 
        } 
 
        DataTable dt = new DataTable(); 
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 
            return ds.Tables[0]; 
        else 
            return null; 
    } 
 
    private DataView getDetails(string ActualNodes) 
    { 
        DataSet ds = new DataSet(); 
 
 
        //string noeuds = ActualNodes.Substring(0, ActualNodes.Length - 1);  
 
        string[] tabNoeuds = ActualNodes.Split(','); 
        string swhen = " CASE "
 
        for (int i = 0; i < tabNoeuds.Length; i++) 
        { 
            swhen += " WHEN EmployeeID = " + tabNoeuds[i] + " THEN " + (i + 1) + " "; 
        } 
        swhen += " END "; 
 
        string sql = "SELECT [EmployeeID], [LastName], [FirstName], [Title], [ReportsTo] FROM [Employees] Where EmployeeID in (" + ActualNodes + ")"; 
        sql += " ORDER BY " + swhen; 
        using (SqlConnection conn = new SqlConnection()) 
        { 
            conn.ConnectionString = GetConnectionString(); 
            SqlCommand cmd = new SqlCommand(sql, conn); 
            conn.Open(); 
 
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd); 
            sqlAdapter.Fill(ds); 
        } 
 
        DataView dv = new DataView(ds.Tables[0]); 
        return dv; 
    } 
 
 
 
    static private string GetConnectionString() 
    { 
        return "server=STATION00374;database=Northwind;Integrated Security=SSPI;"; 
    } 
 
 
    protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        if (Session["RadGrid1_SelectedValue"] != null) 
            Session.Remove("RadGrid1_SelectedValue"); 
 
        Session["RadGrid1_SelectedValue"] = RadGrid1.SelectedValue.ToString();  
    } 
 
 
 

0
Dimo
Telerik team
answered on 04 Feb 2010, 04:03 PM
Hi RadTony,

Not sure what do you mean by "parent row". Here are some help articles:

http://www.telerik.com/help/aspnet-ajax/grdputallitemsineditmodenoadditionalrebind.html

http://www.telerik.com/help/aspnet-ajax/grddefaulteditmodeforgriditemsoninitialload.html

Kind regards,
Dimo
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
RadTony
Top achievements
Rank 1
answered on 04 Feb 2010, 04:48 PM
Dimo,

       By parent row, what I mean : the best way to see it is trying the code I pasted just above : 

       when you click on the "edit" button in RadGrid2, only parent row (EmployeeId = 2) gets edited. All other rows won't edit ...

       I think I'll post my problem in a new post ... 

-  RadTony
       
0
Dimo
Telerik team
answered on 09 Feb 2010, 02:28 PM
Hello RadTony,

I hope I have understood you correctly. Using the self hierarchy demo as a starting point, I am sending it to you with some changes.

http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/selfreferencing/defaultcs.aspx

Keep in mind that we do not claim support for this functionality.

The changes in the above demo are:

- set AllowMultiRowEdit="true" for the RadGrid
- use the OnNeedDataSource event handler instead of DataSourceID
- use the OnEditCommand event handler to iterate through all detail table items and switch them to edit mode


Best wishes,
Dimo
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
RadTony
Top achievements
Rank 1
answered on 09 Feb 2010, 04:20 PM
Hi Dimo,

        Thanks for replying me : I see that Telerik team hasn't forgotten me.
         I actually achieved what I needed and I post the solution below. 
         If it's possible to hide the verticall scroll, it could be really great but intuitively, I think it is impossible.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadGridMarche2.aspx.cs" Inherits="RadGridMarche2" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head> 
   <title></title
    <style type="text/css"
      .StaticColumn 
      {     
          font-family: Arial,Verdana, Helvetica; 
        font-size: 14px; 
        font-weight: normal;    
          border: none; 
          position: relative; 
      }  
  
        .RadGrid td{padding:0}  
         
       .rgExpand   
        {   
          background:url('Images/CollapsedButton.gif') center no-repeat !important;        
        }   
       
       .rgCollapse   
        {   
          background:url('Images/ExpandedButton.gif') center no-repeat !important;   
        }  
         
.RadGrid_Vista tr.DetailRow 
    background:#ebe4d6; 
 
.DetailTable_Vista 
    background:#f4ede1; 
 
.RadGrid_Vista .DetailTable_Vista .GridHeader_Vista, 
.RadGrid_Vista .DetailTable_Vista .ResizeHeader_Vista 
    padding-top:2px; 
    padding-bottom:2px; 
    background-position:0 -6px; 
}         
    </style>      
</head> 
<body> 
    <form runat="server" id="mainForm" method="post">         
      <telerik:RadScriptManager ID="RadScriptManager1" runat="server" /> 
        <!-- content start --> 
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"
            <AjaxSettings> 
                <telerik:AjaxSetting AjaxControlID="RadGrid1"
                    <UpdatedControls> 
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />   
                        <telerik:AjaxUpdatedControl ControlID="RadGrid2" />                       
                    </UpdatedControls> 
                </telerik:AjaxSetting> 
            </AjaxSettings> 
        </telerik:RadAjaxManager> 
        <!-- DataSourceID="SqlDataSource1" OnNeedDataSource="RadGrid1_NeedDataSource"--> 
        <br /> 
            <asp:Label ID="LblRadgrid1_height" runat="server" Width="900px" BackColor="red"></asp:Label> 
        <br /> 
        <table border="0" cellspacing="0" cellpadding="0" style="height:auto;">  
        <tr> 
        <td style="vertical-align:top;height:auto;">         
        <telerik:RadGrid ID="RadGrid1" Skin="Vista" OnPreRender="RadGrid1_PreRender"  OnNeedDataSource="RadGrid1_NeedDataSource" 
                runat="server" OnColumnCreated="RadGrid1_ColumnCreated" 
            OnItemCreated="RadGrid1_ItemCreated" OnItemDataBound="RadGrid1_ItemDataBound"  
                Width="100%" onselectedindexchanged="RadGrid1_SelectedIndexChanged"
            <MasterTableView  HierarchyDefaultExpanded="false" EnableNoRecordsTemplate="false" HierarchyLoadMode="ServerBind" AllowSorting="true" Width="800px" 
                DataKeyNames="EmployeeID" ExpandCollapseColumn-ButtonType="ImageButton" ExpandCollapseColumn-Display="true" 
                ExpandCollapseColumn-ExpandImageUrl="Images/CollapsedButton.gif" ExpandCollapseColumn-CollapseImageUrl="Images/ExpandedButton.gif"
                <ItemStyle Wrap="false" Width="200px" BackColor="Azure" Height="17px" BorderWidth="1"/> 
                <AlternatingItemStyle Wrap="false" BackColor="BlanchedAlmond" Width="200px" Height="17px" BorderWidth="1"/> 
                <HeaderStyle Wrap="false" Width="1500px" Height="20px"/> 
                 
                <SelfHierarchySettings ParentKeyName="ReportsTo" KeyName="EmployeeID"/>  
            </MasterTableView> 
             
            <ClientSettings AllowExpandCollapse="true" EnablePostBackOnRowClick="true" EnableRowHoverStyle="true">       
            <Selecting AllowRowSelect="true" />       
            </ClientSettings> 
             
        </telerik:RadGrid> 
        </td> 
        <td style="vertical-align:top;height:100%;">  
         
            <table style="vertical-align:top" cellpadding="0" cellspacing="0">  
                <tr valign="top"
                   <td valign="top">  
                        <div style="width:800px;overflow:scroll; vertical-align:top;"
                            <telerik:RadGrid ID="RadGrid2" EnableViewState="true" AllowAutomaticUpdates="True"  
                             OnUpdateCommand="RadGrid2_UpdateCommand"  OnNeedDataSource="RadGrid2_NeedDataSource" 
                             runat="server" AutoGenerateColumns="false" AllowSorting="False" OnItemDataBound="RadGrid2_ItemDataBound"  
                             AllowMultiRowSelection="False" Width="800px" GridLines="None"
                <MasterTableView ItemStyle-Wrap="false" Name="Employees" EditItemStyle-Height="0px"  
                 DataKeyNames="EmployeeID" AllowAutomaticUpdates="true" EditMode="InPlace">                    
                <HeaderStyle Wrap="false" Height="20px"/>             
                <AlternatingItemStyle Height="17px" Width="200px" BackColor="BlanchedAlmond" Wrap="false" />  
                <ItemStyle Height="17px" Width="100px" BackColor="Azure" Wrap="false" />          
                <EditItemStyle Height="15px" Wrap="false"/>    
                    <Columns>  
                    <telerik:GridTemplateColumn>  
                    <ItemTemplate> </ItemTemplate> 
                    <EditItemTemplate></EditItemTemplate
                    </telerik:GridTemplateColumn>                          
                        <telerik:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" HeaderButtonType="TextButton" ReadOnly="true" 
                            DataField="EmployeeID" UniqueName="EmployeeID" ItemStyle-Height="17px" FooterStyle-Height="0px" HeaderStyle-Height="0px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="LastName" HeaderText="LastName" HeaderButtonType="TextButton" ItemStyle-Width="200px" 
                            DataField="LastName" UniqueName="LastName" ItemStyle-Height="17px" FooterStyle-Height="0px" HeaderStyle-Height="0px" HeaderStyle-Width="200px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="FirstName" HeaderText="FirstName" HeaderButtonType="TextButton" ItemStyle-Width="200px" 
                            DataField="FirstName" UniqueName="FirstName" ItemStyle-Height="17px" FooterStyle-Height="0px" HeaderStyle-Height="0px" HeaderStyle-Width="200px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="Title" HeaderText="Title" HeaderButtonType="TextButton" ItemStyle-Width="200px" 
                            DataField="Title" UniqueName="Title" ItemStyle-Height="17px" FooterStyle-Height="0px" HeaderStyle-Height="0px" HeaderStyle-Width="200px"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn SortExpression="ReportsTo" HeaderText="ReportsTo" HeaderButtonType="TextButton" ReadOnly="true" 
                            DataField="ReportsTo" UniqueName="ReportsTo" ItemStyle-Height="17px" FooterStyle-Height="0px" HeaderStyle-Height="0px"
                        </telerik:GridBoundColumn>  
                        <telerik:GridEditCommandColumn Display="true" CancelText="C" UpdateText="U" ItemStyle-Height="17px" FooterStyle-Height="0px"  EditImageUrl="Images/ExpandedButton.gif" AutoPostBackOnFilter="true">                         
                        </telerik:GridEditCommandColumn>                                                       
                    </Columns>       
 
                 
                </MasterTableView> 
                <clientsettings allowexpandcollapse="false" EnableRowHoverStyle="true">                 
                        <Scrolling AllowScroll="false" UseStaticHeaders="true"></Scrolling>                                                                                    
                </clientsettings>      
                        
            </telerik:RadGrid> 
                        </div> 
                    </td> 
                </tr> 
            </table> 
        </td> 
         
        </tr>         
        </table>         
        <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
            ProviderName="System.Data.SqlClient" SelectCommand="SELECT EmployeeID, LastName, FirstName, Title, ReportsTo from Employees" 
            runat="server" UpdateCommand="UPDATE [Employees] SET [LastName] = ?, [Company] = ? [FirstName] = ?, [Title]= ? WHERE [EmployeeID] = ?" 
             OldValuesParameterFormatString="original_{0}" ConflictDetection="CompareAllValues"
            <UpdateParameters> 
                <asp:Parameter Name="LastName" Type="String" /> 
                <asp:Parameter Name="FirstName" Type="String" /> 
                <asp:Parameter Name="Title" Type="String" /> 
                <asp:Parameter Name="original_LastName" Type="String" /> 
                <asp:Parameter Name="original_FirstName" Type="String" /> 
                <asp:Parameter Name="original_Title" Type="String" /> 
            </UpdateParameters>            
         </asp:SqlDataSource> 
        <!-- content end --> 
    </form> 
</body> 
</html> 
 
 
 
 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
using System.Reflection; 
using System.Data; 
using System.Data.SqlClient; 
 
public partial class RadGridMarche2 : System.Web.UI.Page 
    //Get the connectionstring from the webconfig and declare a global SqlConnection "SqlConnection"      
    public static string connectionString = GetConnectionString(); 
    public SqlConnection SqlConnection = new SqlConnection(connectionString); 
 
    //Declare a global DataTable dtTable1      
    public static DataTable dtTable1; 
    //Declare a global SqlDataAdapter SqlDataAdapter1      
    public SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter(); 
    //Declare a global SqlCommand SqlCommand      
    public SqlCommand SqlCommand1 = new SqlCommand(); 
 
    //Declare a global DataTable dtTable2      
    public static DataTable dtTable2; 
    //Declare a global SqlDataAdapter SqlDataAdapter2      
    public SqlDataAdapter SqlDataAdapter2 = new SqlDataAdapter(); 
    //Declare a global SqlCommand SqlCommand2      
    public SqlCommand SqlCommand2 = new SqlCommand(); 
 
    protected void RadGrid2_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
    { 
        string ActualNodes = string.Empty; 
 
        if (RadGrid1.Items.Count > 0) 
        { 
            foreach (GridDataItem dataItem in RadGrid1.Items) 
                if (dataItem.Visible) 
                    ActualNodes += dataItem["EmployeeID"].Text + ","; 
        } 
 
        if (ActualNodes.Length > 0) 
        { 
            ActualNodesActualNodes = ActualNodes.Substring(0, ActualNodes.Length - 1); 
 
            RadGrid2.Controls.Clear(); 
            DataView dv = getDetails(ActualNodes); 
            dtTable2 = dv.Table; 
            RadGrid2.DataSource = dtTable2
        } 
 
    }  
 
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
    { 
        //Populate the Radgrid      
        dtTable1 = new DataTable(); 
        //Open the SqlConnection      
        SqlConnection.Open(); 
        try 
        { 
            //Select Query to populate the RadGrid with data from table Employees.      
            string selectQuery = "SELECT EmployeeID, LastName, FirstName, Title, ReportsTo from Employees"
            SqlDataAdapter1.SelectCommand = new SqlCommand(selectQuery, SqlConnection); 
            SqlDataAdapter1.Fill(dtTable1); 
            RadGrid1.DataSource = dtTable1
        } 
        finally 
        { 
            //Close the SqlConnection      
            SqlConnection.Close(); 
        } 
 
    } 
 
    protected void RadGrid2_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) 
    { 
        //Get the GridEditableItem of the RadGrid      
        GridEditableItem eeditedItem = e.Item as GridEditableItem; 
        //Get the primary key value using the DataKeyValue.      
        string EmployeeID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["EmployeeID"].ToString(); 
        //Access the textbox from the edit form template and store the values in string variables.      
        string LastName = (editedItem["LastName"].Controls[0] as TextBox).Text; 
        string FirstName = (editedItem["FirstName"].Controls[0] as TextBox).Text; 
        string Title = (editedItem["Title"].Controls[0] as TextBox).Text; 
 
        try 
        { 
            //Open the SqlConnection      
            SqlConnection.Open(); 
            //Update Query to update the Datatable       
            string updateQuery = " UPDATE Employees " + 
                                 " SET    LastName='" + LastName + "'," + 
                                 "        FirstName='" + FirstName + "'," + 
                                 "        Title='" + Title + "'" + 
                                 " WHERE  EmployeeID='" + EmployeeID + "'"; 
            SqlCommand1.CommandText = updateQuery
            SqlCommand1.Connection = SqlConnection
            SqlCommand1.ExecuteNonQuery(); 
            //Close the SqlConnection      
            SqlConnection.Close(); 
 
 
        } 
        catch (Exception ex) 
        { 
            RadGrid2.Controls.Add(new LiteralControl("Unable to update Employee. Reason: " + ex.Message)); 
            e.Canceled = true
        } 
        finally 
        { 
            RadGrid2.EditIndexes.Clear(); 
        } 
 
    }  
   /* 
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)  
        {  
            TextBox tb = (e.Item as GridEditFormItem)["Column2"].Controls[0] as TextBox;  
            tbtb.Text = tb.Text.Substring(0, 5);  
        }  
    */ 
 
 
    protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem && e.Item.IsInEditMode) 
        { 
            TextBox tbLastName = (e.Item as GridDataItem)["LastName"].Controls[0] as TextBox; 
            tbLastName.Height = Unit.Pixel(11); 
            tbLastName.Style["Font-size"] = "11px"; 
            //tbLastName.Style["padding-top"] = "1px"; 
            tbLastName.Style["Width"] = "100px"; 
 
            TextBox tbFirstName = (e.Item as GridDataItem)["FirstName"].Controls[0] as TextBox; 
            tbFirstName.Height = Unit.Pixel(11); 
            tbFirstName.Style["Font-size"] = "11px"; 
            //tbFirstName.Style["padding-top"] = "1px"; 
            tbFirstName.Style["Width"] = "100px"; 
 
            TextBox tbTitle = (e.Item as GridDataItem)["Title"].Controls[0] as TextBox; 
            tbTitle.Height = Unit.Pixel(11); 
            tbTitle.Style["Font-size"] = "11px"; 
            //tbTitle.Style["padding-top"] = "1px"; 
            tbTitle.Style["Width"] = "150px"; 
        } 
    } 
 
    public void Page_Load(object sender, EventArgs e) 
    { 
        //bindGrid2(); 
        
        if (Assembly.GetAssembly(typeof(ScriptManager)).FullName.IndexOf("3.5") != -1) 
        { 
            RadGrid1.MasterTableView.FilterExpression = @"it[""ReportsTo""] = Convert.DBNull"; 
        } 
        else 
        { 
            RadGrid1.MasterTableView.FilterExpression = "ReportsTo IS NULL"
        } 
         
        if (RadGrid1.MasterTableView.DetailTables != null) 
        { 
            RadGrid1.MasterTableView.DetailTables[0].AlternatingItemStyle.BackColor = System.Drawing.Color.Azure  ; 
            RadGrid1.MasterTableView.DetailTables[0].ItemStyle.BackColor = System.Drawing.Color.BlanchedAlmond; 
 
            if (RadGrid1.MasterTableView.DetailTables[0].DetailTables[0] != null) 
            { 
                RadGrid1.MasterTableView.DetailTables[0].DetailTables[0].AlternatingItemStyle.BackColor = System.Drawing.Color.Azure; 
                RadGrid1.MasterTableView.DetailTables[0].DetailTables[0].ItemStyle.BackColor = System.Drawing.Color.BlanchedAlmond; 
            } 
        } 
         
 
    } 
 
    public void Page_PreRenderComplete(object sender, EventArgs e) 
    { 
        HideExpandColumnRecursive(RadGrid1.MasterTableView); 
    } 
 
    public void HideExpandColumnRecursive(GridTableView tableView) 
    { 
        GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView); 
        foreach (GridNestedViewItem nestedViewItem in nestedViewItems) 
        { 
            foreach (GridTableView nestedView in nestedViewItem.NestedTableViews) 
            { 
                nestedView.Style["border"] = "0"; 
 
                Button MyExpandCollapseButton = (Button)nestedView.ParentItem.FindControl("MyExpandCollapseButton"); 
                if (nestedView.Items.Count == 0) 
                { 
                    if (MyExpandCollapseButton != null) 
                    { 
                        MyExpandCollapseButton.Style["visibility"] = "hidden"; 
                    } 
                    nestedViewItem.Visible = false
                } 
                else 
                { 
                    if (MyExpandCollapseButton != null) 
                    { 
                        MyExpandCollapseButton.Style.Remove("visibility"); 
                    } 
                } 
 
                if (nestedView.HasDetailTables) 
                { 
                    HideExpandColumnRecursive(nestedView); 
                } 
            } 
        } 
    } 
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        CreateExpandCollapseButton(e.Item, "EmployeeID"); 
 
        if (e.Item is GridHeaderItem && e.Item.OwnerTableView != RadGrid1.MasterTableView) 
        { 
            e.Item.Style["display"] = "none"; 
        } 
 
        if (e.Item is GridNestedViewItem) 
        { 
            e.Item.Cells[0].Visible = false
 
            Table table = (Table)e.Item.OwnerTableView.Controls[0]; 
 
            GridItem prevItem = (GridItem)table.Rows[table.Rows.Count - 1]; 
            // Why -3? We need to skip the NestedViewItem and the preceding DataItem's invisible EditItem 
 
            if (prevItem.ItemType == GridItemType.AlternatingItem) 
            { 
                e.Item.CssClass = "DetailAltRow"
            } 
            else 
            { 
                e.Item.CssClass = "DetailRow"
            } 
             
            e.Item.Cells[0].CssClass = "DetailRowCell"
            e.Item.Cells[1].CssClass = "DetailRowCell"
              
        } 
 
    
    } 
 
    protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e) 
    { 
        if (e.Column is GridExpandColumn) 
        { 
            e.Column.Visible = false
        } 
        else if (e.Column is GridBoundColumn) 
        { 
            e.Column.HeaderStyle.Width = Unit.Pixel(100); 
        } 
    } 
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        CreateExpandCollapseButton(e.Item, "EmployeeID"); 
    } 
 
    public void CreateExpandCollapseButton(GridItem item, string columnUniqueName) 
    { 
        if (item is GridDataItem) 
        { 
            if (item.FindControl("MyExpandCollapseButton") == null) 
            { 
                Button button = new Button(); 
                button.Click += new EventHandler(button_Click); 
                button.CommandName = "ExpandCollapse"
                button.CssClass = (item.Expanded) ? "rgCollapse" : "rgExpand"; 
                button.ID = "MyExpandCollapseButton"
 
                if (item.OwnerTableView.HierarchyLoadMode == GridChildLoadMode.Client) 
                { 
                    string script = String.Format(@"$find(""{0}"")._toggleExpand(this, event); return false;", item.Parent.Parent.ClientID); 
 
                    button.OnClientClick = script
                } 
 
                int level = item.ItemIndexHierarchical.Split(':').Length; 
                if (level > 1) 
                { 
                    button.Style["margin-left"] = level + 10 + "px"; 
                    button.Style["height"] = "15px"; 
                } 
 
 
                TableCell cell = ((GridDataItem)item)[columnUniqueName]; 
                cell.Controls.Add(button); 
 
                LiteralControl liSpace = new LiteralControl("&nbsp;"); 
                cell.Controls.Add(liSpace); 
 
                cell.Controls.Add(new LiteralControl(((GridDataItem)item).GetDataKeyValue(columnUniqueName).ToString())); 
                cell.Style["height"] = "15px"; 
 
            } 
        } 
    } 
 
    protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
         
        string ActualNodes = string.Empty; 
 
        foreach (GridDataItem dataItem in RadGrid1.Items) 
        { 
            if (dataItem.Visible) 
            { 
                ActualNodes += dataItem["EmployeeID"].Text + ","; 
            } 
        } 
 
         
         
 
        if (ActualNodes.Length > 0) 
            ActualNodesActualNodes = ActualNodes.Substring(0, ActualNodes.Length - 1); 
 
        RadGrid2.Controls.Clear(); 
        RadGrid2.DataSource = getDetails(ActualNodes); 
        RadGrid2.DataBind(); 
        //Response.Write("<script> alert(" + sTest + ");</script>");  
         
        // if a row is selected in RadGrid1, then select the same row in RadGrid2 
        if (Session["RadGrid1_SelectedValue"] != null) 
        { 
            for (int m = 0; m < RadGrid2.Items.Count; m++) 
            { 
                if (RadGrid2.Items[m]["EmployeeID"].Text.Equals(Session["RadGrid1_SelectedValue"].ToString())) 
                { 
                    RadGrid2.Items[m].Selected = true
                    break; 
                } 
            } 
            Session.Remove("RadGrid1_SelectedValue"); 
        }    
          
    } 
 
 
    void button_Click(object sender, EventArgs e) 
    { 
        ((Button)sender).CssClass = (((Button)sender).CssClass == "rgExpand") ? "rgCollapse" : "rgExpand"; 
        LblRadgrid1_height.Text = RadGrid1.Height.Value.ToString(); 
        //Response.Write("<script language='javascript'> document.alert(" + RadGrid1.Height.Value.ToString() + ");</script>"); 
    } 
 
 
    private void bindGrid2() 
    { 
 
        RadGrid2.Controls.Clear(); 
        string initialNodes = string.Empty; 
        for (int m = 0; m < RadGrid1.Items.Count; m++) 
        { 
            if (!RadGrid1.Items[m]["EmployeeID"].Text.ToLower().Equals("&nbsp;") && RadGrid1.Items[m].Visible) 
                initialNodes += RadGrid1.Items[m]["EmployeeID"].Text + ","; 
        } 
 
        if (initialNodes.Length > 0) 
        { 
            RadGrid2.DataSource = getEmployees(initialNodes.Substring(0, initialNodes.Length - 1)); 
        } 
        else 
            RadGrid2.DataSource = getEmployees(""); 
 
        RadGrid2.DataBind(); 
    } 
 
    private DataView getEmployees(string initialNodes) 
    { 
        DataSet ds = new DataSet(); 
 
        string sqlFinal = string.Empty; 
        if (!initialNodes.Equals(string.Empty)) 
        { 
            sqlFinal = "Where EmployeeID in (" + initialNodes + ")"; 
        } 
 
        using (SqlConnection conn = new SqlConnection()) 
        { 
            conn.ConnectionString = GetConnectionString(); 
            SqlCommand cmd = new SqlCommand("SELECT top 1 [EmployeeID], [LastName], [FirstName], [Title], [ReportsTo] FROM [Employees] " + sqlFinal, conn); 
            conn.Open(); 
 
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd); 
 
            sqlAdapter.Fill(ds); 
        } 
 
        DataView dv = new DataView(ds.Tables[0]); 
        return dv; 
    } 
 
    private DataTable getChildNodesByID(string ParentID) 
    { 
        DataSet ds = new DataSet(); 
        string sql = "SELECT [EmployeeID], [LastName], [FirstName], [Title], [ReportsTo] FROM [Employees] Where ReportsTo = " + ParentID; 
 
        using (SqlConnection conn = new SqlConnection()) 
        { 
            conn.ConnectionString = GetConnectionString(); 
            SqlCommand cmd = new SqlCommand(sql, conn); 
            conn.Open(); 
 
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd); 
            sqlAdapter.Fill(ds); 
        } 
 
        DataTable dt = new DataTable(); 
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 
            return ds.Tables[0]; 
        else 
            return null; 
    } 
 
    private DataView getDetails(string ActualNodes) 
    { 
        if (ActualNodes.Length == 0) return null; 
 
        DataSet ds = new DataSet(); 
 
 
        //string noeuds = ActualNodes.Substring(0, ActualNodes.Length - 1);  
 
        string[] tabNoeuds = ActualNodes.Split(','); 
        string swhen = " CASE "
 
        for (int i = 0; i < tabNoeuds.Length; i++) 
        { 
            swhen += " WHEN EmployeeID = " + tabNoeuds[i] + " THEN " + (i + 1) + " "; 
        } 
        swhen += " END "; 
 
        string sql = "SELECT [EmployeeID], [LastName], [FirstName], [Title], [ReportsTo] FROM [Employees] Where EmployeeID in (" + ActualNodes + ")"; 
        sql += " ORDER BY " + swhen; 
        using (SqlConnection conn = new SqlConnection()) 
        { 
            conn.ConnectionString = GetConnectionString(); 
            SqlCommand cmd = new SqlCommand(sql, conn); 
            conn.Open(); 
 
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd); 
            sqlAdapter.Fill(ds); 
        } 
 
        DataView dv = new DataView(ds.Tables[0]); 
        return dv; 
    } 
 
 
 
    static private string GetConnectionString() 
    { 
        return "server=STATION00374;database=Northwind;Integrated Security=SSPI;"; 
    } 
 
 
    protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        if (Session["RadGrid1_SelectedValue"] != null) 
            Session.Remove("RadGrid1_SelectedValue"); 
 
        Session["RadGrid1_SelectedValue"] = RadGrid1.SelectedValue.ToString();  
    } 
 
    protected void RadGrid2_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e) 
    { 
        string item = getItemName(e.Item.OwnerTableView.Name); 
        string field = getFieldName(e.Item.OwnerTableView.Name); 
        if (e.Exception != null) 
        { 
            e.KeepInEditMode = true
            e.ExceptionHandled = true
            DisplayMessage(item + " " + e.Item[field].Text + " cannot be updated. Reason: " + e.Exception.Message); 
        } 
        else 
        { 
            DisplayMessage(item + " " + e.Item[field].Text + " updated"); 
        } 
    } 
 
    private String getItemName(string tableName) 
    { 
        switch (tableName) 
        { 
            case ("Employees"): 
                { 
                    return "Employees"; 
                } 
            default: return ""; 
        } 
    } 
 
    private String getFieldName(string tableName) 
    { 
        switch (tableName) 
        { 
            case ("Employees"): 
                { 
                    return "EmployeeID"; 
                } 
            default: return ""; 
        } 
    } 
 
    private void DisplayMessage(string text) 
    { 
        RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text))); 
    } 
 

Tags
Grid
Asked by
RadTony
Top achievements
Rank 1
Answers by
RadTony
Top achievements
Rank 1
Dimo
Telerik team
Share this question
or