add the selected node value from the impeded RadDropDownTree in a RadGrid cell

1 Answer 40 Views
Grid TreeView
khaled
Top achievements
Rank 1
Iron
khaled asked on 16 Oct 2021, 03:42 PM

I'm trying to select an item value from DropDownTree which impeded in a RadGrid , in the add new record in the radGrid then select the item from dropdownTree , but i can't find a way to access the radgrid cell to update its value by the  DropDownTree value , 

the scenario is get the itemCode value from DropDownTree  then search in Database to get the Item Price then put the price in the RadGrid

 Protected Sub RadDropDownTree1_EntryAdded(sender As Object, e As DropDownTreeEntryEventArgs)
        GetItemPrice(e.Entry.Value)
    End Sub
    Private Sub GetItemPrice(Item_code As Int32)
        Dim ConnString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(ConnString)
        Dim command As SqlCommand = New SqlCommand()
        Dim myreader As SqlDataReader
        command.CommandText = "[dbo].[FindItemPrice]"
        command.CommandType = CommandType.StoredProcedure
        conn.Open()
        command.Connection = conn
        command.Parameters.AddWithValue("ItemCode", Item_code)

        Try
            myreader = command.ExecuteReader()

            If myreader.Read() Then
                Dim ItemGrid As RadGrid = CType(Page.Master.FindControl("MainContent").FindControl("RadGrid2"), RadGrid)



                For Each item As GridDataItem In ItemGrid.MasterTableView.Items
                    Dim tx As TextBox = TryCast(item.FindControl("Price"), TextBox)
                    tx.Text = myreader("Unit_Price")
                Next

            Else

            End If

            conn.Close()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
Attila Antal
Telerik team
commented on 20 Oct 2021, 11:46 AM

Hi Khaled. Before I make any wild guess, I would like to know how the Grid is configured. So please share the complete code (Grid markup and code-behind including the DropDownTree). Based on that information I will have a better idea and I might give you the correct answer.
khaled
Top achievements
Rank 1
Iron
commented on 22 Oct 2021, 10:29 AM

Thanks for you replay

this is the grid definition in aspx  

                                                                                                                                   

  <telerik:RadGrid RenderMode="Lightweight" ID="RadGrid2" CssClass="GridTreeAlign"
            ShowStatusBar="true" runat="server"
            AllowPaging="True"
            AllowAutomaticUpdates="true"
            AllowAutomaticInserts="True" 
            AllowAutomaticDeletes="True"
            OnItemCreated="RadGrid2_ItemCreated"
            GridLines="None" AutoGenerateColumns="false"
            PageSize="5" DataSourceID="SqlDataSource3">

            <MasterTableView Width="100%" AutoGenerateColumns="False" style="text-align: -webkit-right;"
                DataKeyNames="ItemHeaderId,Id" EditMode="InPlace" CommandItemDisplay="Top"
              ShowFooter="false"  DataSourceID="SqlDataSource3" InsertItemDisplay="Top"
             InsertItemPageIndexAction="ShowItemOnFirstPage">
                 <CommandItemSettings ExportToPdfText="Export to PDF" AddNewRecordText="أضافة عنصر جديد"  CancelChangesText="تراجع" SaveChangesText="حفظ" RefreshText="أعادة تنشيط"></CommandItemSettings>
                <Columns>
                       <telerik:GridBoundColumn ReadOnly="true" DataField="ItemHeaderId" Display="false" HeaderText="كود ت" SortExpression="ItemHeaderId"  ItemStyle-Width="100px"  HeaderStyle-Width="100px"
                        UniqueName="ItemHeaderId">
                             <HeaderStyle ForeColor="Silver"></HeaderStyle>
                    </telerik:GridBoundColumn>
                      <telerik:GridBoundColumn ReadOnly="true" DataField="Id" HeaderText="كود ص" Display="false" SortExpression="Id"  ItemStyle-Width="100px"  HeaderStyle-Width="100px"
                        UniqueName="Id">
                            <HeaderStyle ForeColor="Silver"></HeaderStyle>
                    </telerik:GridBoundColumn>
                     <telerik:GridTemplateColumn HeaderText="إسم الصنف"  ItemStyle-Width="400px">
                        <ItemTemplate>
                            <%#DataBinder.Eval(Container.DataItem, "Item_Name")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadDropDownTree RenderMode="Lightweight" runat="server" dir="rtl"  CssClass="dropdownTree" ID="RadDropDownTreeTest" 

                                DataTextField="Item_Name"
                                DataFieldParentID="Parent_Id"
                                DataFieldID="Child_Id" 
                                DataValueField="Item_Code"
                                DataSourceID="SqlDataSource4" 
                                OnDataBound="RadDropDownTreeTest_DataBound"
                                 AutoPostBack="true"

                                ExpandNodeOnSingleClick="true" 

                                OnEntryAdded="RadDropDownTree1_EntryAdded"

                                SelectedValue='<%#Bind("Item_Code") %>' DefaultMessage="اختـــار الصـــنف من فضـــــلك"
                                DefaultValue="1" DropDownSettings-AutoWidth="Enabled"
                                DropDownSettings-OpenDropDownOnLoad="True"                                
                                EnableFiltering="True" 

                                FilterSettings-EmptyMessage="أكتب اسم الصنف" 
                                Font-Bold="True"
                                DropDownSettings-Width="400px" Width="400px">

                                <DropDownSettings OpenDropDownOnLoad="true" 
                                    Width="400px"  CloseDropDownOnSelection="true" AutoWidth="Enabled" />
                     <ExpandAnimation Type="InBounce" Duration="300" />
                        <CollapseAnimation Type="OutQuint" Duration="200" />
                            </telerik:RadDropDownTree>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn DataField="Price" HeaderText="سعر الصنف" SortExpression="Price" 
                        UniqueName="Price" HeaderStyle-Width="100px" ItemStyle-Width="100px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Qty" HeaderText="الكمية" SortExpression="Qty" HeaderStyle-Width="100px" ItemStyle-Width="100px"
                        UniqueName="Qty">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Total" HeaderText="الإجمالي" ReadOnly="true" SortExpression="Total" HeaderStyle-Width="100px" ItemStyle-Width="100px"
                        UniqueName="Total">
                    </telerik:GridBoundColumn>
                     <telerik:GridEditCommandColumn  HeaderText="تعديل" UniqueName="EditCommandColumn">
                          <HeaderStyle Width="20px" ></HeaderStyle>
                        <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton"></ItemStyle>
                    </telerik:GridEditCommandColumn>
                     <telerik:GridButtonColumn ConfirmText="حذف هذا الصنف نهائيا؟" HeaderText="حذف"
                        CommandName="Delete" Text="حذف" UniqueName="DeleteColumn"   ConfirmDialogType="RadWindow" ButtonType="FontIconButton" ConfirmTitle="برنامج المثمن">
                        <HeaderStyle Width="20px" ></HeaderStyle>
                        <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton"></ItemStyle>
                    </telerik:GridButtonColumn>
                </Columns>
            </MasterTableView>
                 <ClientSettings AllowKeyboardNavigation="true" >
                <Selecting AllowRowSelect="true"></Selecting>
            </ClientSettings>
            <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
        </telerik:RadGrid>

then he code behind

Protected Sub RadDropDownTree1_EntryAdded(sender As Object, e As DropDownTreeEntryEventArgs)
        GetItemPrice(e.Entry.Value)
        ' RadGrid2.RenderClientStateField()
    End Sub
    Private Sub GetItemPrice(Item_code As Int32)
        Dim ConnString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(ConnString)
        Dim command As SqlCommand = New SqlCommand()
        Dim myreader As SqlDataReader
        command.CommandText = "[dbo].[FindItemPrice]"
        command.CommandType = CommandType.StoredProcedure
        conn.Open()
        command.Connection = conn
        command.Parameters.AddWithValue("ItemCode", Item_code)

        Try
            myreader = command.ExecuteReader()

            If myreader.Read() Then

                Dim ItemGrid As RadGrid = CType(Page.Master.FindControl("MainContent").FindControl("RadGrid2"), RadGrid)
                Dim dataItm As GridDataItem = TryCast(ItemGrid.MasterTableView.Items(-1), GridDataItem)
                Dim cell As TableCell = dataItm("price")
                cell.Text = Convert.ToString(myreader("Unit_Price"))
                cell.Focus()
                cell.BorderColor = Color.Red


            End If

            conn.Close()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

 

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 22 Oct 2021, 12:58 PM

Hi Khaled,

The server Bind() expression is responsible for selecting the Value in the DropDownTree if it has this value in its data source.

SelectedValue='<%#Bind("Item_Code") %>'

Anytime you open an item for editing, if the DropDownTree has an item with the same value coming from the "Item_Code" field of the Grid's source, this item will be automatically selected.

You can observe this behavior in our online example here: DropDownTree in RadGrid

When you click update, the Bind() expression will also submit the selected value to the Update Method. The "Item_Code" field in the Grid's data source will be updated with the SelectedValue coming from the DropDownTree.

If you use individual data sources for both the Grid and DropDownTree, then you must make sure that any data coming from the Grid must also exist in the DropDownTree. Otherwise, you will not be able to select a value that comes from the Grid but it does not exist in the DropDownTree.

The majority of the Code you shared seems to be okay. You do not need the "RadDropDownTree1_EntryAdded" for this scenario unless you need to do something additional.

Regards,
Attila Antal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

khaled
Top achievements
Rank 1
Iron
commented on 23 Oct 2021, 08:39 PM

Thank you for your replay , I may did not said my problem in a clear way sorry for that , My case is i want to select item from the tree and its bind to item_code and i need o get the item_price and show it in the grid Item Price Field ,

thats my case , also is there is any way to save two value in the raddropdowntree  like name and price ,

in my code i make  "RadDropDownTree1_EntryAdded"  to get the price according to Item_code and i can't display in the grid field .. , another issue is that i can't get the index of the new record,

Attila Antal
Telerik team
commented on 27 Oct 2021, 10:19 AM

Khaled,

You will need to decide whether you want to send the DropDownTree's Selected value to the server to be updated, or, you want the Item price based on the selected value.

The "SelectedValue='<%#Bind("Item_Code") %>'" will update the "Item_Code" field in the database with the DropDownTree's value. If you do not want to do that, you will need to remove the SelectedValue property.

If you want to update the database with the Item price based on the DropDownTree's selected value, you will need to do it programmatically.

Protected Sub RadGrid1_UpdateCommand(sender As Object, e As GridCommandEventArgs)
    Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
    
    '.....    rest of the code ....

    'Update new values
    Dim newValues As New Hashtable()
    e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)

    Dim ddTree = TryCast(editedItem.FindControl("RadDropDownTree1"), RadDropDownTree)

    If Not ddTree Is Nothing Then
        newValues("Item_Price") = GetItemPrice(ddTree.SelectedValue)
    End If

    '....  rest of the code ...

End Sub

 

Tags
Grid TreeView
Asked by
khaled
Top achievements
Rank 1
Iron
Answers by
Attila Antal
Telerik team
Share this question
or