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

RadGrid Hierachical expand newly added row

7 Answers 110 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eva
Top achievements
Rank 1
Eva asked on 02 Jul 2013, 10:20 AM
Hello,
I have a hierarchical radgrid with two levels.
Insert,update and delte commands are not automated.
  
My proble is that i want the insertedItem to be expanded just right after insert is done.
Could you please help?
  
thanks in advance,
Eva Xanthou

7 Answers, 1 is accepted

Sort by
0
Eva
Top achievements
Rank 1
answered on 03 Jul 2013, 06:25 AM
anyone????
0
Princy
Top achievements
Rank 1
answered on 03 Jul 2013, 08:50 AM
Hi Eva,

By default when you rebind a parent table in hierarchical grid the settings/expanded state for the nested tables will be lost. This is because the refresh action resets all previous settings applied for the nested controls to avoid viewstate inconsistencies/unexpected behavior.
Please have a look at this code library which will help you with your requirement.
Retain expanded/selected state in hierarchy on rebind

Thanks,
Princy
0
Eva
Top achievements
Rank 1
answered on 03 Jul 2013, 09:04 AM
Hello,
what i want is the item in the first level that is being inserted by the button 'Add New Record' to be expanded after the PerformInsert Command, not to retain expanded state.

thanks,
Eva


0
Princy
Top achievements
Rank 1
answered on 03 Jul 2013, 09:20 AM
Hi Eva,
Please try the following code snippet.

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="EmployeeID" 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["Northwind_newConnectionString3"].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,
Princy
0
Eva
Top achievements
Rank 1
answered on 03 Jul 2013, 09:24 AM
Dear Princy,

thanks for your quick response.
i came accross this answer yesterday and i tested.
the problem with this is the paging.
my grid has paging and the other one is that i only want to expand the row added only after insert.
with this code every time the newest row would be expanded.

is ther anything else?

0
Eva
Top achievements
Rank 1
answered on 04 Jul 2013, 06:12 AM
???
0
Princy
Top achievements
Rank 1
answered on 04 Jul 2013, 07:16 AM
Hi Eva,

I guess you want only the last item to be expanded and close all other expanded rows.
Please try the below code snippet.Let me know if you have any issue
ASPX is the same as i posted before.

C#:

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)
    {
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            if (item.Expanded == true)
            {
                item.Expanded = false;
            }
        }
        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,
Princy
Tags
Grid
Asked by
Eva
Top achievements
Rank 1
Answers by
Eva
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or