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

[Solved] hierarchy in grid doesn't work

12 Answers 264 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mydatafactory
Top achievements
Rank 1
Mydatafactory asked on 30 May 2008, 02:07 PM
I would like to build a three-level hierarchy like the sample, but I got stuck.

In the tables I've made a relation between the mentioned tables (primary key and foreign key), but the grid keeps on telling  me there's no child record to display. What do I do wrong or did I miss something?
    <telerik:RadGrid ID="gvProjects" runat="server" DataSourceID="dsProject"   
        GridLines="None" Skin="Office2007" AllowAutomaticDeletes="True"   
        AllowAutomaticInserts="True" AllowAutomaticUpdates="True"   
        AutoGenerateColumns="False">  
        <PagerStyle Mode="NextPrevAndNumeric" /> 
<MasterTableView CommandItemDisplay="Bottom" DataKeyNames="ProjectId">  
<DetailTables> 
<telerik:GridTableView DataKeyNames="ProjectId" DataSourceID="dsFiles" runat="server" CommandItemDisplay="Top" Name="tblFiles">  
<ParentTableRelation> 
<telerik:GridRelationFields DetailKeyField="ProjectId" MasterKeyField="ProjectId" /> 
</ParentTableRelation> 
 
<RowIndicatorColumn Visible="true">  
<HeaderStyle Width="20px"></HeaderStyle> 
</RowIndicatorColumn> 
 
<ExpandCollapseColumn Visible="true" Resizable="False">  
<HeaderStyle Width="20px"></HeaderStyle> 
</ExpandCollapseColumn> 
 
<EditFormSettings> 
<PopUpSettings ScrollBars="None"></PopUpSettings> 
</EditFormSettings> 
</telerik:GridTableView> 
</DetailTables> 
 
    <asp:SqlDataSource ID="dsProject" runat="server"   
        ConnectionString="<%$ ConnectionStrings:DBPortal %>"   
        DeleteCommand="DELETE FROM [tblProjects] WHERE [ProjectId] = @ProjectId"   
        InsertCommand="INSERT INTO [tblProjects] ([ProjectName], [ProjectDescription], [Remarks], [CustomerId], [ContractNummer], [DataSupplierId]) VALUES (@ProjectName, @ProjectDescription, @Remarks, @CustomerId, @ContractNummer, @DataSupplierId)"   
        SelectCommand="SELECT * FROM [tblProjects]"   
        UpdateCommand="UPDATE [tblProjects] SET [ProjectName] = @ProjectName, [ProjectDescription] = @ProjectDescription, [Remarks] = @Remarks, [CustomerId] = @CustomerId, [ContractNummer] = @ContractNummer, [DataSupplierId] = @DataSupplierId WHERE [ProjectId] = @ProjectId">  
        <DeleteParameters> 
            <asp:Parameter Name="ProjectId" Type="Int32" /> 
        </DeleteParameters> 
        <UpdateParameters> 
            <asp:Parameter Name="ProjectName" Type="String" /> 
            <asp:Parameter Name="ProjectDescription" Type="String" /> 
            <asp:Parameter Name="Remarks" Type="String" /> 
            <asp:Parameter Name="CustomerId" Type="Int32" /> 
            <asp:Parameter Name="ContractNummer" Type="Int64" /> 
            <asp:Parameter Name="DataSupplierId" Type="Int32" /> 
            <asp:Parameter Name="ProjectId" Type="Int32" /> 
        </UpdateParameters> 
        <InsertParameters> 
            <asp:Parameter Name="ProjectName" Type="String" /> 
            <asp:Parameter Name="ProjectDescription" Type="String" /> 
            <asp:Parameter Name="Remarks" Type="String" /> 
            <asp:Parameter Name="CustomerId" Type="Int32" /> 
            <asp:Parameter Name="ContractNummer" Type="Int64" /> 
            <asp:Parameter Name="DataSupplierId" Type="Int32" /> 
        </InsertParameters> 
    </asp:SqlDataSource>    
 
<asp:SqlDataSource ID="dsFiles" runat="server"   
        ConnectionString="<%$ ConnectionStrings:DBPortal %>"   
        SelectCommand="SELECT * FROM [tblFiles] WHERE ([ProjectId] = @ProjectId)"   
        DeleteCommand="DELETE FROM [tblFiles] WHERE [FileId] = @FileId">  
      <SelectParameters> 
          <asp:SessionParameter Name="ProjectId" SessionField="ProjectId" Type="Int32" /> 
      </SelectParameters> 
      <DeleteParameters> 
          <asp:Parameter Name="FileId" Type="Int32" /> 
      </DeleteParameters> 
        </asp:SqlDataSource> 

12 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 02 Jun 2008, 06:51 AM
Hi,

Can you try setting AutoGenerateColumns to true and see whether it is working ?

ASPX:
<telerik:RadGrid ID="gvProjects" runat="server" DataSourceID="dsProject"      
        GridLines="None" Skin="Office2007" AllowAutomaticDeletes="True"      
        AllowAutomaticInserts="True" AllowAutomaticUpdates="True"      
        AutoGenerateColumns="True" >     
 


Thanks
Princy.
0
Mydatafactory
Top achievements
Rank 1
answered on 02 Jun 2008, 08:24 AM
That solves the problem, but (as you may have guessed) I would not autogenerate columns.
0
Shinu
Top achievements
Rank 2
answered on 02 Jun 2008, 09:43 AM
Hi,

If you dont want AutoGeneratedColumns you have to go for Manual Column Binding. Go through the following help article for more details.
Adding columns from design-time

Shinu.

0
Mydatafactory
Top achievements
Rank 1
answered on 03 Jun 2008, 07:26 AM
I have a two-level grid now, but still I can't get the third level.

What code behind do I have to use: radgrid1.SelectedIndexes.Add(1, 0, 1, 0, 1) on Page_Load (this is my current code) or detaildatabinding?

The sample demonstrates it with the SelectedIndexes.

0
Shinu
Top achievements
Rank 2
answered on 03 Jun 2008, 07:41 AM
0
Daniel
Telerik team
answered on 03 Jun 2008, 10:12 AM
Hi A. Bennen,

Please take a look at the following links:
Hierarchical data-binding using declarative relations
Three-level hierarchy
Hierarchy - What you should know
Understanding hierarchical grid structure
Creating hierarchical grids from design-time

Kind regards,
Daniel
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Mydatafactory
Top achievements
Rank 1
answered on 03 Jun 2008, 12:03 PM
After struggling a few days and despite the sample tutorials, I still doesn't work:
I have 3 Datasources:
Level 1. tblProject (ProjectId)
Level 2. tblTasks (ProjectId)
Level 3. FilesPerTask (TaskId)

The first two levels are no problem, but the third one won't open properly. in the code-behind doesn't recognize the TaskId as a String. I checked the .aspx code and it looks OK.

part of .aspx

<

MasterTableView CommandItemDisplay="Bottom" DataKeyNames="ProjectId" AllowPaging="True">

<DetailTables>

<telerik:GridTableView runat="server" DataKeyNames="ProjectId"

CommandItemDisplay="Bottom" Name="Tasks"

PageSize="10" PagerStyle-Mode="NextPrevAndNumeric" AllowPaging="True">

<ParentTableRelation>

<telerik:GridRelationFields DetailKeyField="ProjectId"

MasterKeyField="ProjectId" />

</ParentTableRelation>

<DetailTables>

<telerik:GridTableView runat="server" DataKeyNames="TaskId"

CommandItemDisplay="Bottom" Name="FilesToTask">

<ParentTableRelation>

<telerik:GridRelationFields DetailKeyField="TaskId" MasterKeyField="TaskId" />

</ParentTableRelation>

</telerik:GridTableView>

</DetailTables>


this is my code behind:

Protected

Sub gvProjects_DetailTableDataBind(ByVal source As Object, ByVal e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles gvProjects.DetailTableDataBind

Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)

Select Case e.DetailTableView.Name

Case "Tasks"

Dim ProjectId As String = dataItem.GetDataKeyValue("ProjectId").ToString()

e.DetailTableView.DataSource = GetDataTable(

"SELECT * FROM tblTasks WHERE ProjectId = '" & ProjectId & "'")

Case "FilesToTask"

Dim TaskId As String = dataItem.GetDataKeyValue("TaskId").ToString()

e.DetailTableView.DataSource = GetDataTable(

"SELECT * FROM FilesPerTask WHERE TaskId = " & TaskId)

End Select

End Sub

Protected Sub gvProjects_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles gvProjects.NeedDataSource

If Not e.IsFromDetailTable Then

gvProjects.DataSource = GetDataTable(

"SELECT * FROM tblProjects")

End If

End Sub

Public Function GetDataTable(ByVal query As String) As DataTable

Dim ConnString As String = ConfigurationManager.ConnectionStrings("DBPortal").ConnectionString

Dim connection1 As New SqlConnection(ConnString)

Dim adapter1 As New SqlDataAdapter

adapter1.SelectCommand =

New SqlCommand(query, connection1)

Dim table1 As New DataTable

connection1.Open()

Try

adapter1.Fill(table1)

Finally

connection1.Close()

End Try

Return table1

End Function

0
Daniel
Telerik team
answered on 06 Jun 2008, 08:05 AM
Hello A.Bennen,

I suggest you to make the following modifications to your aspx code:

<MasterTableView CommandItemDisplay="Bottom" DataKeyNames="ProjectId" AllowPaging="True"
    <DetailTables> 
        <telerik:GridTableView runat="server" DataKeyNames="TaskID" CommandItemDisplay="Bottom" 
            Name="Tasks" PageSize="10" PagerStyle-Mode="NextPrevAndNumeric" AllowPaging="True"
            <DetailTables> 
                <telerik:GridTableView runat="server" CommandItemDisplay="Bottom" 
                    Name="FilesToTask"
                </telerik:GridTableView> 
            </DetailTables> 
        </telerik:GridTableView> 
    </DetailTables> 
</MasterTableView> 

Let us know whether this helps.

Kind regards,
Daniel
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Mydatafactory
Top achievements
Rank 1
answered on 06 Jun 2008, 10:22 AM
Hello Daniel,

The modification to my .aspx code works! Thanx.

The next challenge is to modify the grid so that it looks like the demo: http://www.telerik.com/demos/aspnet/Grid/Examples/Hierarchy/ThreeLevel/DefaultCS.aspx

I'm interested in the edit- en deletebutton and the editform inside the grid. Next to that I would like not to autogenerate the columns, but to make use of the gridcolumnbound.

Is there something I have to think about when I would to use this features in my three-level grid?
0
Daniel
Telerik team
answered on 09 Jun 2008, 02:49 PM
Hello A.Bennen,

I propose you the following possible approach:

<Columns> 
    <telerik:GridEditCommandColumn ButtonType="ImageButton" EditImageUrl="Edit.gif"
    </telerik:GridEditCommandColumn> 
    <telerik:GridClientDeleteColumn ButtonType="ImageButton" ImageUrl="Delete.gif"
    </telerik:GridClientDeleteColumn> 
</Columns> 
<EditFormSettings> 
    <EditColumn UpdateImageUrl="Update.gif" ButtonType="ImageButton"
    </EditColumn> 
    <EditColumn CancelImageUrl="Cancel.gif" ButtonType="ImageButton"
    </EditColumn> 
</EditFormSettings> 

Kind regards,
Daniel
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Mydatafactory
Top achievements
Rank 1
answered on 09 Jun 2008, 04:44 PM
Thanx for your .aspx code. It helped me a lot. Of course :) I have another question:

when inserting an item in a detailtable you must define the parent key field for newly inserted detail row manually. I used the code behind of the sample 'update/insert/delete in hierarchy', but it doesn't work properly. Has this something to do with the DatakeyValues. In the .aspx I do not have a ParentTableRelation and GridRelationFields.

If "Projects".Equals(e.Item.OwnerTableView.Name) Then

Dim parentItem As GridDataItem = DirectCast(e.Item.OwnerTableView.ParentItem, GridDataItem)

dsTasks.InsertParameters(

"ProjectId").DefaultValue = parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("ProjectId").ToString()

ElseIf "Files".Equals(e.Item.OwnerTableView.Name) Then

Dim parentItem As GridDataItem = DirectCast(e.Item.OwnerTableView.ParentItem, GridDataItem)

dsFiles.InsertParameters(

"ProjectId").DefaultValue = parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("ProjectId").ToString()

End If

0
Daniel
Telerik team
answered on 11 Jun 2008, 11:59 AM
Hi A.Bennen,

Try to change your code to:
If "Tasks".Equals(e.Item.OwnerTableView.Name) Then 
    Dim parentItem As GridDataItem = DirectCast(e.Item.OwnerTableView.ParentItem, GridDataItem) 
    dsTasks.InsertParameters("ProjectId").DefaultValue = parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("ProjectId").ToString() 
ElseIf "Files".Equals(e.Item.OwnerTableView.Name) Then 
 
    Dim parentItem As GridDataItem = DirectCast(e.Item.OwnerTableView.ParentItem, GridDataItem) 
    dsFiles.InsertParameters("TaskID").DefaultValue = parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("TaskID").ToString() 
End If 
 

Best wishes,
Daniel
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Mydatafactory
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Mydatafactory
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Daniel
Telerik team
Share this question
or