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

Problems with DetailTable

3 Answers 39 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kurt Kluth
Top achievements
Rank 1
Kurt Kluth asked on 25 Sep 2013, 06:26 PM

Having some difficulties working with the MasterTable and the DetailTable within my Grid.  I have it to the point I would like it but need some minor adjustments and I believe I have what I am looking for.
How do I get the CartID from the MasterTable passed to my DetailTable Datasource. 

<telerik:RadGrid ID="_grdHistory" runat="server" AutoGenerateColumns="False"
                    CellSpacing="0" GridLines="None">
                    <MasterTableView>
                    <DetailTables>
                    <telerik:GridTableView DataKeyNames="CartID" DataSourceID="SqlDataSource3" Width="100%" runat="server" EnableHeaderContextMenu="false">
                        <ParentTableRelation>
                            <telerik:GridRelationFields DetailKeyField="CartID" MasterKeyField="CartID">
                            </telerik:GridRelationFields>
                        </ParentTableRelation>
                        <Columns>
                            <telerik:GridBoundColumn SortExpression="CartID" HeaderText="CartID" DataField="CartID" UniqueName="CartID" Display="True">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn SortExpression="ItemName" HeaderText="Item Name" DataField="ItemName" UniqueName="ItemName">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn SortExpression="ItemCnt" HeaderText="Item Cnt" DataField="ItemCnt" UniqueName="ItemCnt">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn SortExpression="TotItemCost" HeaderText="Item Cnt" DataField="TotItemCost" UniqueName="TotItemCost">
                            </telerik:GridBoundColumn>
                        </Columns>
                    </telerik:GridTableView>
 
                    </DetailTables>
 
 
                    <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                    </RowIndicatorColumn>
 
                    <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                    </ExpandCollapseColumn>
 
                        <Columns>
                            <telerik:GridHyperLinkColumn AllowSorting="False"
                                FilterControlAltText="Filter InvoiceID column" HeaderText="INVOICE #"
                                UniqueName="InvoiceID" DataNavigateUrlFields="InvoiceID"
                                DataNavigateUrlFormatString="OrderHistoryDetail.aspx?ID={0}"
                                DataTextField="InvoiceID" Target="_blank">
                            </telerik:GridHyperLinkColumn>
                            <telerik:GridBoundColumn DataField="Order_Date"
                                FilterControlAltText="Filter column1 column" HeaderText="ORDER DATE"
                                UniqueName="column1" DataFormatString="{0:MM/dd/yyyy}">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Order_Amt"
                                FilterControlAltText="Filter column1 column" HeaderText="ORDER AMOUNT"
                                UniqueName="column1" DataFormatString="{0:c}" ItemStyle-HorizontalAlign="Right">
                                <ItemStyle HorizontalAlign="Right"></ItemStyle>
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="OrderedByName"
                                FilterControlAltText="Filter column1 column" HeaderText="ORDERED BY"
                                UniqueName="column1">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Shipped"
                                FilterControlAltText="Filter column1 column" HeaderText="SHIPPED"
                                UniqueName="column1" DataFormatString="{0:MM/dd/yyyy}">
                            </telerik:GridBoundColumn>
                            <telerik:GridHyperLinkColumn AllowSorting="False" DataTextField="FedExTrack"
                                DataNavigateUrlFields="FedExTrack"
                                FilterControlAltText="Filter column3 column"
                                ImageUrl="~/images/FedEx-Shipping-Box-icon.png"
                                DataNavigateUrlFormatString="http://www.fedex.com/Tracking?action=track&;tracknumbers={0}"
                                UniqueName="FedExTrack" Target="_blank">
                            </telerik:GridHyperLinkColumn>
                            <telerik:GridBoundColumn DataField="Approved"
                                FilterControlAltText="Filter column1 column" 
                                UniqueName="Approved" ItemStyle-HorizontalAlign="Right" Display="False">
                                <ItemStyle HorizontalAlign="Right"></ItemStyle>
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="CartID"
                                FilterControlAltText="Filter CartID column" 
                                UniqueName="CartID" ItemStyle-HorizontalAlign="Right" Display="False">
                                <ItemStyle HorizontalAlign="Right"></ItemStyle>
                            </telerik:GridBoundColumn>
                        </Columns>
                    </MasterTableView>
                </telerik:RadGrid>
            </td></tr>
            </table>
        </td>
    </tr>
</table>
                    <asp:SqlDataSource ID="SqlDataSource3" ConnectionString="<%$ ConnectionStrings:PHConn %>"
                        SelectCommand="sp_getCartDetail" SelectCommandType="StoredProcedure"
                        runat="server">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="_grdHistory" Name="CartID"
                                PropertyName="SelectedValue" Type="Int32" />
                        </SelectParameters>
                    </asp:SqlDataSource>

Also what I would like to do is if column "Approved" is NULL/blank then change the 1st column to "Pending".  Likewise with FedExTrack, I would like to show it as blank and not show the image.  How can I go about accomplishing these two items.  Below is the code I started with but when I added the DetailTable it broke my logic.

Sub PopulateData()
    Try
        oConn.Open()
        Dim ocmd As New SqlCommand("sp_getOrderHistoryFilter", oConn)
        ocmd.CommandType = CommandType.StoredProcedure
        With ocmd.Parameters
            .Add(New SqlParameter("@asi_num", Session("asi_num")))
            .Add(New SqlParameter("@Date1", DBNull.Value))
            .Add(New SqlParameter("@Date2", DBNull.Value))
            .Add(New SqlParameter("@Area", DBNull.Value))
        End With
        reader = ocmd.ExecuteReader()
        Me._grdHistory.DataSource = reader
        Me._grdHistory.DataBind()
    Catch ex As Exception
 
    Finally
        oConn.Close()
    End Try
End Sub
 
Private Sub _grdHistory_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles _grdHistory.ItemDataBound
    If TypeOf e.Item Is GridDataItem Then
        'This does not appear to be working at this time.
        'Purpose is to display different text if the order has not been approved yet
        'If e.Item.Cells(8).Text = " " Then
        '    e.Item.Cells(1).Text = "Pending"
        '    e.Item.Cells(1).Font.Italic = True
        'End If
 
        'If e.Item.Cells(6).Text = " " Then 'Hide Shipping if there is no data
        '    e.Item.Cells(7).Text = ""
        'End If
    End If
 
End Sub







3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 26 Sep 2013, 09:41 AM
Hi,

Please try the below sample code snippet that shows a similar scenario as you have described.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" 
    GridLines="None"  AllowPaging="true" OnItemDataBound="RadGrid1_ItemDataBound">
    <MasterTableView DataKeyNames="CustomerID" Name="Parent">
        <DetailTables>
            <telerik:GridTableView DataKeyNames="CustomerID"  Width="100%"
                runat="server" EnableHeaderContextMenu="false" Name="Child">
                <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID">
                    </telerik:GridRelationFields>
                </ParentTableRelation>
                <Columns>
                    <telerik:GridBoundColumn SortExpression="OrderID" HeaderText="OrderID" DataField="OrderID"
                        UniqueName="OrderID" Display="True">
                    </telerik:GridBoundColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
        <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
            <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>
        <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
            <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridHyperLinkColumn AllowSorting="False" FilterControlAltText="Filter CustomerID column"
                HeaderText="CustomerID" UniqueName="CustomerID" DataNavigateUrlFields="CustomerID"
                DataNavigateUrlFormatString="CustomerID.aspx?ID={0}" DataTextField="CustomerID"
                Target="_blank">
            </telerik:GridHyperLinkColumn>
            <telerik:GridBoundColumn DataField="CompanyName" FilterControlAltText="Filter CompanyName column"
                HeaderText="CompanyName" UniqueName="CompanyName">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="City" FilterControlAltText="Filter City column"
                HeaderText="City" UniqueName="City">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Address" FilterControlAltText="Filter Address column"
                HeaderText="Address" UniqueName="Address">
            </telerik:GridBoundColumn>
            <telerik:GridHyperLinkColumn AllowSorting="False" DataTextField="ContactTitle" DataNavigateUrlFields="ContactTitle"
                FilterControlAltText="Filter ContactTitle column" HeaderText="ContactTitle" UniqueName="ContactTitle"
                ImageUrl="~/down.jpg" Target="_blank" DataNavigateUrlFormatString="http://www.fedex.com/Tracking?action=track&;tracknumbers={0}">
            </telerik:GridHyperLinkColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If e.Item.OwnerTableView.Name = "Parent" Then
        If TypeOf e.Item Is GridDataItem Then
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
            Dim city As String = item("City").Text
            If (city Is Nothing) OrElse (city = "") Then
                item("CustomerID").Text = "Pending"
                item("ContactTitle").Text = ""
            End If
        End If
    End If
End Sub

Thanks,
Princy
0
Kurt Kluth
Top achievements
Rank 1
answered on 26 Sep 2013, 01:56 PM
Princy,

Thank you for the code example.  Giving the Master Table and Detail Table names helped out greatly.  On the first load of the page, the "Pending" and blank field for the FedExTracking, display as expected.  However when I expand to load the detail the Parent table resets itself.  What must I do to prevent it from resetting itself?

I have attached images to show the results of what I am encountering. 
0
Accepted
Princy
Top achievements
Rank 1
answered on 27 Sep 2013, 05:33 AM
Hi Kurth,

Please move the code in to the PreRender event of the RadGrid,so as to keep the changes.

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
       foreach (GridDataItem item in RadGrid1.Items)
       {
           if (item.OwnerTableView.Name == "Parent")
           {
               string city = item["City"].Text;
               if ((city == null) || (city == " "))
               {
                   item["CustomerID"].Text = "Pending";
                   item["ContactTitle"].Text = "";
               }
           }
       }
   }

Thanks,
Princy
Tags
Grid
Asked by
Kurt Kluth
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Kurt Kluth
Top achievements
Rank 1
Share this question
or