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

Expand Child item after insert the parent

2 Answers 80 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Raja M
Top achievements
Rank 1
Raja M asked on 29 Mar 2012, 11:14 AM
Hi,

I have radgrid with detailtable.
I want to exapnd the child item after inserting the parent.
Any one know the idea..?


Regards,
Raja.M

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 30 Mar 2012, 10:59 AM
Hi Raja,

Please take a look into the sample code I tried to expand the row after inserting.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnPreRender="RadGrid1_PreRender"
    OnInsertCommand="RadGrid1_InsertCommand" DataSourceID="SqlDataSource1">
    <MasterTableView TableLayout="Fixed" DataKeyNames="EmployeeID" CommandItemDisplay="Top">
        <Columns>
            <telerik:GridBoundColumn UniqueName="EmployeeID" DataField="EmployeeID" HeaderText="EmployeeID" />
            <telerik:GridBoundColumn UniqueName="FirstName" DataField="FirstName" HeaderText="First Name" />
            <telerik:GridBoundColumn UniqueName="LastName" DataField="LastName" HeaderText="Last Name" />
        </Columns>
        <DetailTables>
            <telerik:GridTableView DataKeyNames="TerritoryID" Name="Detail" DataSourceID="SqlDataSource2">
                <Columns>
                    <telerik:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" UniqueName="EmployeeID">
                    </telerik:GridBoundColumn>
                </Columns>
                <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="EmployeeID" MasterKeyField="EmployeeID" />
                </ParentTableRelation>
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>

C#:
public static string connection = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connection);
public SqlCommand SqlCommand = new SqlCommand();
public static Int32 EmpId = 0;
 
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        if (Convert.ToInt32(item.GetDataKeyValue("EmployeeID").ToString()) == EmpId)
        {
            item.Expanded = true;
        }
    }
}
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
    Int32 Id = Convert.ToInt32((insertItem["EmployeeID"].Controls[0] as TextBox).Text);
    string firstname = (insertItem["FirstName"].Controls[0] as TextBox).Text;
    string lastname = (insertItem["LastName"].Controls[0] as TextBox).Text;
    SqlCommand SqlCommand = new SqlCommand("Insert into Employees(EmployeeID,FirstName,LastName) values(@EmployeeID,@FirstName,@LastName)", conn);
    SqlCommand.Parameters.Add(new SqlParameter("@EmployeeID", Id));
    SqlCommand.Parameters.Add(new SqlParameter("@FirstName", firstname));
    SqlCommand.Parameters.Add(new SqlParameter("@LastName", lastname));
    conn.Open();
    int count = SqlCommand.ExecuteNonQuery();
    conn.Close();
    EmpId = Id;
}

Thanks,
Shinu.
0
Raja M
Top achievements
Rank 1
answered on 04 Apr 2012, 10:43 AM
Hi Shinu,

Thanks for your reply.
Its working fine. Thanks a lot..!


Regards,
Raja M
Tags
Grid
Asked by
Raja M
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Raja M
Top achievements
Rank 1
Share this question
or