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

RadGrid Custom Datasource - Need Recommendation

4 Answers 173 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Developer
Top achievements
Rank 1
Developer asked on 09 Nov 2009, 09:15 PM
Here is the scenario:

SQL Database Tables involved:
Contacts
Numbers             (A contact can have multiple numbers)
Addresses           (A contact can have multiple addresses)
PracticeAreas     (A contact can have multiple Practiceareas)

When I combine the tables in a single query I get multiple rows of the same contact when the contact has multiple numbers, addresses, or practice areas.  So I went ahead and built a grid with the datasource from a query that included just the contacts table. 

On the grid ItemDataBound I do calls to the database to pull the correct numbers/addresses/practiceareas for each contact.
I save multiple numbers in a hidden field as a string.  (Example:  "number1+number2+number3" could be a string of 3 numbers for one contact)
(Everything is displayed in a single Template column with multiple controls)

This works good but is pretty resource intensive doing multiple calls to the DB for each record.....

Also on the ItemDataBound event, when e.Item.IsInEditMode I fill my controls in my edit template with the values from my Contact datasource.  However, I am not sure how to access the hidden fields because they are in the ItemTemplate  How do I do that?

Also, does anyone have a better idea on how to display this type of data in a grid and keep track of it for edits and updates?





4 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 12 Nov 2009, 04:52 PM
Hi Developer,

How to access the parent GridDataItem related to the opened edit form when using built-in EditForms mode you can see from this topic from the documentation.

Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Developer
Top achievements
Rank 1
answered on 20 Nov 2009, 02:44 PM
Hello Sebastian,

Thanks for the response.  I actually changed my approach and instead of hiding the hidden fields.  I am storing theach table in session and a single variable for the contactId that is updated whenever the parent grid's EditCommand is fired.  When the child grids need a datasource I use a rowfilter to find the correct records.

However I am having another problem.  Right now I have a Parent Grid with two Child Grids, one that holds Phone Numbers and one that holds the PracticeAreas.

When I click "Edit" on the Parent Grid, both child grids show up with the correct data.   When I clidk "Edit" on a row in the Phone Numbers grid, the grid simply disappears.  I put break points in all my sub procedures and the only one that is firing when I click "Edit" is the "radGridNumbers_ItemCreated" function.  Within this function I checked to make sure my rowfilter in my session had the correct data for the datasource and it did.

NOTE:  This only started happening when I added the second child grid.  Before that Inserts/Updates/Deletes worked fine on the grid.

Here are the 2 child grids:
 <telerik:RadGrid   
                                        ID="radGridNumbers"   
                                        runat="server"   
                                        Skin="WebBlue"   
                                        ShowStatusBar="false" 
                                        AutoGenerateColumns="False"   
                                        GridLines="None"   
                                        OnItemDataBound="radGridNumbers_ItemDataBound"   
                                        OnItemCreated="radGridNumbers_ItemCreated"   
                                        OnDeleteCommand="radGridNumbers_DeleteCommand"   
                                        OnInsertCommand="radGridNumbers_InsertCommand" 
                                        OnUpdateCommand="radGridNumbers_UpdateCommand">  
                                        <HeaderContextMenu EnableTheming="True">  
                                            <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                                        </HeaderContextMenu> 
                                        <MasterTableView EditMode="InPlace" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add New Number" 
                                            CommandItemSettings-RefreshImageUrl="" CommandItemSettings-RefreshText="">  
                                            <CommandItemSettings AddNewRecordText="Add New Number" RefreshText="" /> 
                                            <RowIndicatorColumn> 
                                                <HeaderStyle Width="20px"></HeaderStyle> 
                                            </RowIndicatorColumn> 
                                            <ExpandCollapseColumn> 
                                                <HeaderStyle Width="20px"></HeaderStyle> 
                                            </ExpandCollapseColumn> 
                                            <Columns> 
                                                <telerik:GridEditCommandColumn ButtonType="LinkButton">  
                                                  
                                                </telerik:GridEditCommandColumn> 
                                                <telerik:GridDropDownColumn DataField="Id" DropDownControlType="RadComboBox" HeaderText="Type" 
                                                    ListTextField="Type" ListValueField="Id" UniqueName="Type">  
                                                </telerik:GridDropDownColumn> 
                                                <telerik:GridMaskedColumn DataField="Number" HeaderText="Number" Mask="(###) ###-####" 
                                                    UniqueName="Number">  
                                                </telerik:GridMaskedColumn> 
                                                <telerik:GridMaskedColumn DataField="Extension" HeaderText="Extension" Mask="########" 
                                                    UniqueName="Extension">  
                                                </telerik:GridMaskedColumn> 
                                                <telerik:GridButtonColumn ButtonType="LinkButton" CommandName="Delete" ConfirmText="Delete This Number?" 
                                                    Text="X" UniqueName="Delete">  
                                                    <ItemStyle Font-Bold="true" ForeColor="Red" /> 
                                                </telerik:GridButtonColumn> 
                                            </Columns> 
                                            <EditFormSettings> 
                                                <EditColumn UniqueName="EditCommandColumn1">  
                                                </EditColumn> 
                                            </EditFormSettings> 
                                        </MasterTableView><FilterMenu EnableTheming="True">  
                                            <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                                        </FilterMenu> 
                                    </telerik:RadGrid> 
                                
                                 
                                    <telerik:RadGrid   
                                    ID="RadGridPracticeAreas"   
                                    runat="server"   
                                    AutoGenerateColumns="False" 
                                    EnableAJAX="True"   
                                    GridLines="Horizontal"   
                                    Skin="WebBlue"   
                                    ShowStatusBar="false" 
                                    OnNeedDataSource="RadGridPracticeAreas_NeedDataSource"   
                                    OnItemDataBound="RadGridPracticeAreas_ItemDataBound"   
                                    OnDeleteCommand="RadGridPracticeAreas_DeleteCommand"   
                                    OnInsertCommand="RadGridPracticeAreas_InsertCommand"   
                                    OnUpdateCommand="RadGridPracticeAreas_UpdateCommand">  
                                        <HeaderContextMenu EnableTheming="True">  
                                            <CollapseAnimation Duration="200" Type="OutQuint" /> 
                                        </HeaderContextMenu> 
                                        <MasterTableView CommandItemDisplay="Top" EditMode="InPlace">  
                                            <Columns> 
                                                <telerik:GridEditCommandColumn ButtonType="LinkButton">  
                                                </telerik:GridEditCommandColumn> 
                                                <telerik:GridTemplateColumn DataField="Disciplines" HeaderText="Contact Discipline List" 
                                                    SortExpression="Disciplines" UniqueName="Disciplines">  
                                                    <ItemTemplate> 
                                                          
                                                                    <%#DataBinder.Eval(Container.DataItem, "Abbreviation").ToString.Trim%> 
                                                                    &nbsp;>&nbsp;  
                                                                    <%#DataBinder.Eval(Container.DataItem, "SubPracticeArea").ToString.Trim%> 
            
                                                    </ItemTemplate> 
                                                    <EditItemTemplate> 
                                                        <table> 
                                                            <tr> 
                                                                <td style="border: 0px;">  
                                                                    <asp:HiddenField ID="HiddenFieldId" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "Id").ToString.Trim%>' /> 
                                                                    <asp:HiddenField ID="HiddenFieldPracticeAreaId" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "PracticeAreaId").ToString.Trim%>' /> 
                                                                    <telerik:RadComboBox ID="rcbDisciplines" runat="server" Skin="WebBlue" 
                                                                        Height="300px" AutoPostBack="True" DataSource="<%# GetDisciplines() %>" DataTextField="Abbreviation" 
                                                                        DataValueField="PracticeAreaId" Width="80px" OnSelectedIndexChanged="rcbDisciplines_SelectedIndexChanged">  
                                                                        <CollapseAnimation Duration="200" Type="OutQuint" /> 
                                                                    </telerik:RadComboBox> 
                                                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" 
                                                                        ControlToValidate="rcbDisciplines" InitialValue=""></asp:RequiredFieldValidator> 
                                                                </td> 
                                                                <td style="border: 0px; padding-left: 1px;">  
                                                                <asp:HiddenField ID="HiddenFieldSubPracticeAreaId" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "SubPracticeAreaId").ToString.Trim%>' /> 
                                                                    <telerik:RadComboBox ID="rcbSubDisciplines" Skin="WebBlue" runat="server">  
                                                                    </telerik:RadComboBox> 
                                                                </td> 
                                                            </tr> 
                                                        </table> 
                                                    </EditItemTemplate> 
                                                </telerik:GridTemplateColumn> 
                                                <telerik:GridButtonColumn ButtonType="LinkButton" CommandName="Delete" ConfirmText="Delete This Discipline?" 
                                                    Text="X" UniqueName="Delete">  
                                                    <ItemStyle ForeColor="Red" Font-Bold="true" /> 
                                                </telerik:GridButtonColumn> 
                                            </Columns> 
                                            <CommandItemSettings AddNewRecordText="Add New Discipline" RefreshText=""></CommandItemSettings> 
                                            <ExpandCollapseColumn Visible="False">  
                                                <HeaderStyle Width="20px" /> 
                                            </ExpandCollapseColumn> 
                                            <RowIndicatorColumn Visible="False">  
                                                <HeaderStyle Width="20px" /> 
                                            </RowIndicatorColumn> 
                                            <EditFormSettings> 
                                                <EditColumn UniqueName="EditCommandColumn1">  
                                                </EditColumn> 
                                            </EditFormSettings> 
                                        </MasterTableView><FilterMenu EnableTheming="True">  
                                            <CollapseAnimation Duration="200" Type="OutQuint" /> 
                                        </FilterMenu> 
                                    </telerik:RadGrid> 


Any help is appreciated, I was hoping to get this launched in Beta today.

On a side note.   Is there a way to make the column headers disappear?

Thanks,

Coty
0
Accepted
Sebastian
Telerik team
answered on 25 Nov 2009, 03:24 PM
Hello Developer,

From your code snippet I noticed that you use advanced binding with NeedDataSource event handling for your second grid. Can you please confirm that you do the same for the first grid? This should ensure that the control will be rebound as expected when you trigger editing operation.

To hide the column headers in the grid, simply set the ShowHeader property of its MasterTableView to false.

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Developer
Top achievements
Rank 1
answered on 25 Nov 2009, 08:45 PM
You were absolutely right!  I didn't have my NeedsDataSource event wired up correctly....Thank You, it works perfectly now.

Coty
Tags
Grid
Asked by
Developer
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Developer
Top achievements
Rank 1
Share this question
or