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

Update hierarchy radgrid

5 Answers 62 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ghadeer
Top achievements
Rank 1
ghadeer asked on 20 Nov 2008, 11:16 AM
Hi All,
I have nested radgrid and I want to update field inside the child grid,so how can I access the boundfield  of the childgrid in Update event ???


thanks in advance
ghadeer

5 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 20 Nov 2008, 12:06 PM
Hello Ghadeer,

For DetailTables, you can use the Name property and access the controls in the table on the UpdateCommand.
aspx:
 <telerik:RadGrid ID="RadGrid2" AutoGenerateColumns="false" runat="server" OnUpdateCommand="RadGrid2_UpdateCommand" > 
           
        <MasterTableView Name="Master">        
       <DetailTables> 
        <telerik:GridTableView Name="Detail" DataSourceID="SqlDataSource1" runat="server" > 
        <Columns>     
        <telerik:GridBoundColumn DataField="ProductName" HeaderText="ProductName" UniqueName="ProductName"></telerik:GridBoundColumn> 
         
        <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn> 
        </Columns>           
        </telerik:GridTableView> 
       </DetailTables> 
          ...... 

cs:
protected void RadGrid2_UpdateCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.Item is GridEditableItem && e.Item.OwnerTableView.Name == "Detail") 
        { 
            GridEditableItem editItem = (GridEditableItem)e.Item; 
            TextBox txtbx = (TextBox)editItem["ProductName"].Controls[0]; 
            string strtxt = txtbx.Text; 
        } 
    } 

Thanks
Princy.

0
Vishnu
Top achievements
Rank 2
answered on 03 Mar 2015, 11:58 AM
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="State.aspx.cs" Inherits="Grid.Exam.State" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <div>
    
      
        
         <telerik:RadGrid ID="RadGrid1" runat="server" ShowStatusBar="true" AutoGenerateColumns="False"
            PageSize="7" AllowSorting="True" AllowMultiRowSelection="False" AllowPaging="True"
            OnDetailTableDataBind="RadGrid1_DetailTableDataBind" OnNeedDataSource="RadGrid1_NeedDataSource"
            OnPreRender="RadGrid1_PreRender"  Width="40%" onbatcheditcommand="RadGrid1_BatchEditCommand">
             <PagerStyle Mode="NumericPages" ></PagerStyle>
              <MasterTableView DataKeyNames="StateId" AllowMultiColumnSorting="True" EditMode="InPlace" CommandItemDisplay="TopAndBottom" Name="StatesList" >

               <DetailTables>
                    <telerik:GridTableView DataKeyNames="DistrictId" Name="DistrictName" Width="100%" EditMode="InPlace" CommandItemDisplay="TopAndBottom">
                    
                        <DetailTables>
                            <telerik:GridTableView DataKeyNames="DistrictId" Name="LocationName" Width="100%" EditMode="InPlace" CommandItemDisplay="TopAndBottom">
                                <Columns>
                                <telerik:GridBoundColumn SortExpression="LocationId" HeaderText="LocationId" HeaderButtonType="TextButton"
                                        DataField="LocationId" Visible="false">
                                    </telerik:GridBoundColumn>
                                <telerik:GridTemplateColumn HeaderText="SI.No">
                                <ItemTemplate>
                                <%# Container.ItemIndex + 1 %>
                                </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                  
                                  <telerik:GridTemplateColumn UniqueName="locationname" SortExpression="locationname" HeaderText="Location Name" DataField="locationname" HeaderButtonType="TextButton">
                                  
                                  <ItemTemplate>
                                  <asp:Label ID="lbllocation" runat="server" Text= '<%#bind("LocationName") %>'></asp:Label>
                                 
                                  </ItemTemplate>
                                <EditItemTemplate>
                                <asp:TextBox ID ="txtlocation" runat="server" >
                                
                                </asp:TextBox>
                                 <asp:LinkButton ID="lbtnInsert" runat="server" CommandName="Insert" Text="Insert"></asp:LinkButton>
                        <asp:LinkButton ID="lblcancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                                </EditItemTemplate>
                                  </telerik:GridTemplateColumn>
                                                                   <telerik:GridButtonColumn UniqueName="Edit" CommandName="Edit" Text="Edit"></telerik:GridButtonColumn>
                                      <telerik:GridButtonColumn UniqueName="Delete" CommandName="Delete" Text="Delete"></telerik:GridButtonColumn>  
                                </Columns>
                            </telerik:GridTableView>
                        </DetailTables>
                        <Columns>
                            <telerik:GridBoundColumn SortExpression="DistrictId" HeaderText="DistrictId" HeaderButtonType="TextButton"
                                DataField="DistrictId" Visible="false">
                            </telerik:GridBoundColumn>
                            <telerik:GridTemplateColumn HeaderText="SI.No">
                                <ItemTemplate>
                                <%# Container.ItemIndex + 1 %>
                                </ItemTemplate>
                                </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn UniqueName="districtname" SortExpression="districtname" HeaderText="District Name" DataField="districtname" HeaderButtonType="TextButton">
                                  <ItemTemplate>
                                  <asp:Label ID="lbldistrict" runat="server" Text= '<%#bind("DistrictName") %>'></asp:Label>
                                  </ItemTemplate>
                                  <EditItemTemplate>
                                <asp:TextBox ID ="txtdistrict" runat="server"></asp:TextBox>
                                          <asp:LinkButton ID="lbtnInsert" runat="server" CommandName="Insert" Text="Insert"></asp:LinkButton>
                        <asp:LinkButton ID="lblcancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                  
                                </EditItemTemplate>
                                  </telerik:GridTemplateColumn>
                                                                    <telerik:GridButtonColumn UniqueName="Edit" CommandName="Edit" Text="Edit"></telerik:GridButtonColumn>
                                      <telerik:GridButtonColumn UniqueName="Delete" CommandName="Delete" Text="Delete"></telerik:GridButtonColumn>
                           
                        </Columns>
                    </telerik:GridTableView>
                </DetailTables>
                <Columns>
                    <telerik:GridBoundColumn SortExpression="StateId" HeaderText="StateId" HeaderButtonType="TextButton"
                        DataField="StateId" Visible="false">

        
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn HeaderText="SI.No">
                                <ItemTemplate>
                                <%# Container.ItemIndex + 1 %>
                                </ItemTemplate>
                                </telerik:GridTemplateColumn>
                     <telerik:GridTemplateColumn UniqueName="statename" SortExpression="statename" HeaderText="State Name" DataField="statename" HeaderButtonType="TextButton">
                                  <ItemTemplate>
                                  <asp:Label ID="lblstatename" runat="server" Text= '<%#bind("StateName") %>'></asp:Label>
                                  
                                  </ItemTemplate>
                                  <EditItemTemplate>
                                <asp:TextBox ID ="txtstate" runat="server"></asp:TextBox>
                                        <asp:LinkButton ID="lbtnInsert" runat="server" CommandName="Insert" Text="Insert"></asp:LinkButton>
                        <asp:LinkButton ID="lblcancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                                </EditItemTemplate>
                                
                                  </telerik:GridTemplateColumn>
                                  <telerik:GridButtonColumn UniqueName="Edit" CommandName="Edit" Text="Edit"></telerik:GridButtonColumn>
                                      <telerik:GridButtonColumn UniqueName="Delete" CommandName="Delete" Text="Delete"></telerik:GridButtonColumn>
                   
                </Columns>
              
               </MasterTableView>

            
            </telerik:RadGrid>
    </div>
    </form>
</body>
</html>



This Is my design page...How to insert,delete and update rows..???





















0
Kostadin
Telerik team
answered on 06 Mar 2015, 08:31 AM
Hello Vishnu,

Please check out the following forum thread which elaborates more on this matter.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Vishnu
Top achievements
Rank 2
answered on 14 Mar 2015, 12:00 PM
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Grid.Exam
{
    public partial class StudentDetail : System.Web.UI.Page
    {
    
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadDropDownTree1.DataFieldID = "ID";
                RadDropDownTree1.DataFieldParentID = "ParentID";
                RadDropDownTree1.DataValueField = "Value";
                RadDropDownTree1.DataTextField = "Text";
                RadDropDownTree1.DataSource = GetData();
                RadDropDownTree1.DataBind();

            }
            }
        public DataTable GetData()
        {



            string str = "select ParentID=null,Stateid as Value,StateName as Text from State ";
            str += "Union all ";
            str += "select StateId as ParentID,DistrictId as Value,DistrictName as Text from District ";
            str += "where StateId in(Select StateId from District) ";
            str += "Union all ";
            str += "select DistrictID as ParentID,LocationId as Value,LocationName as Text from ";

            str += "Location where districtId in(select DistrictId from Location)";
            SqlCommand cmd = new SqlCommand(str, con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);


            DataTable table = new DataTable();
            table.Columns.Add("ID");
            table.Columns.Add("ParentID");
            table.Columns.Add("Value");
            table.Columns.Add("Text");
            if (ds.Tables[0].Rows.Count != 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = table.NewRow();
                    //int j = i + 1;
                    //dr["ID"] = j;
                    string st = "";
                    //if (st == ds.Tables[0].Rows[i]["ParentID"].ToString())
                    //{
                    //    dr["ParentID"] = "null";
                    //}
                    //else
                    //{
                    //    dr["ParentID"] = ds.Tables[0].Rows[i]["ParentID"].ToString();
                    //}
                    if (st == ds.Tables[0].Rows[i]["ParentID"].ToString())
                    {
                        dr["ParentID"] = "";
                    }
                    else
                    {
                        //{
                        //    dr["ParentID"] = ds.Tables[0].Rows[i]["ParentID"].ToString();
                        //}

                        dr["ParentID"] = ds.Tables[0].Rows[i]["ParentID"].ToString();

                    }
                    dr["ID"] = ds.Tables[0].Rows[i]["Value"].ToString();

                    dr["Value"] = ds.Tables[0].Rows[i]["Value"].ToString();
                    dr["Text"] = ds.Tables[0].Rows[i]["Text"].ToString();
                    table.Rows.Add(dr);

                }
            }

                //string id = "";
                ////int lastid = 0;
                //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                //{
                //    DataRow dr = table.NewRow();
                //    if (id == ds.Tables[0].Rows[i]["ParentID"].ToString())
                //    {
                //        dr["ID"] = ds.Tables[0].Rows[i]["ID"].ToString();
                //        //lastid = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"].ToString());
                //        id = ds.Tables[0].Rows[i]["ParentID"].ToString();
                //        dr["ParentID"] = null;
                //    }
                //    else
                //    {
                //        dr["ID"] = ds.Tables[0].Rows[i]["Value"].ToString();
                //        dr["ParentID"] = ds.Tables[0].Rows[i]["ParentID"].ToString();
                //        //lastid = lastid + 1;
                //        //dr["ID"] = lastid + 1;
                //        //lastid = lastid + 1;
                //    }

                //    dr["Value"] = ds.Tables[0].Rows[i]["Value"].ToString();
                //    dr["Text"] = ds.Tables[0].Rows[i]["Text"].ToString();
                //    table.Rows.Add(dr);



            //DataTable table = new DataTable();
            //table.Columns.Add("ID");
            //table.Columns.Add("ParentID");
            //table.Columns.Add("Value");
            //table.Columns.Add("Text");
            //table.Rows.Add(new String[] { "22", null, "22", "Tamilnadu" });
            //table.Rows.Add(new String[] { "23", null, "23", "Kerala" });
            //table.Rows.Add(new String[] { "24", null, "24", "Karnataka" });
            //table.Rows.Add(new String[] { "11", "22", "11", "Kanyakumari" });
            //table.Rows.Add(new String[] { "12", "22", "12", "Chennai" });
            //table.Rows.Add(new String[] { "13", "22", "13", "Nellai" });
            //table.Rows.Add(new String[] { "14", "23", "14", "TVM" });


            //table.Rows.Add(new String[] { "15", "24", "15", "Bangalore" });
            //table.Rows.Add(new String[] { "16", "11", "16", "Nagercoil" });
            //table.Rows.Add(new String[] { "17", "12", "17", "Pallavaram" });
            //table.Rows.Add(new String[] { "18", "13", "18", "Nellai" });
            //table.Rows.Add(new String[] { "19", "14", "19", "trivandrum" });
            //table.Rows.Add(new String[] { "20", "15", "20", "Mysore" });

            return table;
        
        }       
        }
}
    




Error:
____Server Error in '/' Application.This constraint cannot be enabled as not all values have corresponding parent values.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: This constraint cannot be enabled as not all values have corresponding parent values.

Source Error: 

Line 27: RadDropDownTree1.DataTextField = "Text";
Line 28: RadDropDownTree1.DataSource = GetData();
Line 29: RadDropDownTree1.DataBind();
Line 30: 
Line 31: }
Source File: E:\Vishnu\TElerik\Samples\Grid\Grid\Grid\Exam\StudentDetail.aspx.cs    Line: 29 

Stack Trace: 

[ArgumentException: This constraint cannot be enabled as not all values have corresponding parent values.]
System.Data.ConstraintCollection.AddForeignKeyConstraint(ForeignKeyConstraint constraint) +1825731
System.Data.ConstraintCollection.Add(Constraint constraint, Boolean addUniqueWhenAddingForeign) +413
System.Data.DataSetRelationCollection.AddCore(DataRelation relation) +794
System.Data.DataRelationCollection.Add(DataRelation relation) +153
System.Data.DataRelationCollection.Add(DataColumn parentColumn, DataColumn childColumn) +45
Telerik.Web.UI.ControlDataBinder.BindToDataTable(DataTable table, String dataFieldID, String dataFieldParentID) +97
Telerik.Web.UI.HierarchicalControlItemContainer.PerformDataBinding(IEnumerable data) +425
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
Telerik.Web.UI.DropDownTree.EmbeddedTreeAdapter.DataBind() +101
Telerik.Web.UI.RadDropDownTree.DataBind() +46
Grid.Exam.StudentDetail.Page_Load(Object sender, EventArgs e) in E:\Vishnu\TElerik\Samples\Grid\Grid\Grid\Exam\StudentDetail.aspx.cs:29
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Version Information: Microsoft .NET Framework Version:2.0.50727.6421; ASP.NET Version:2.0.50727.6412
0
Kostadin
Telerik team
answered on 18 Mar 2015, 10:43 AM
Hi Vishnu,

I noticed that you are facing an issue with RadDropDownTree control and not with RadGrid. I would recommend in this case to open a new forum thread for the appropriate product as this way we can track the issues better and provide you with a faster response.
Nevertheless this exception is observe when the ParentID of the root items is not null. Keep in mind that you need at least one item with a null value that can be used as a root item of the DropDownTree. If you examine the data in the following help article you will noticed that the first two records have a null value and they will be used as a root items.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
ghadeer
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Vishnu
Top achievements
Rank 2
Kostadin
Telerik team
Share this question
or