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

TreeView in Grid Form Template Questions

6 Answers 120 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Shawn
Top achievements
Rank 1
Shawn asked on 03 Feb 2009, 08:40 PM
I have a grid that when in edit and insert mode uses a FromTemplate.  In that template I want to have a tree view that has checkboxes.  The items will load from a SQL data source, but how do I take a column when in edit mode and check the appropriate boxes based on an array item.

My database column is named "banned_sources" and will have a value like so:

Either "0" for no banned sources, or a string like "5,61,64,91,137,139,161"

The numbers in the array match the "source_id" of my datasource.  How do select the matches in my tree view if they are present in the array?

ASPX code:
<td> 
            Banned Lead Sources:</td> 
        <td> 
            <asp:SqlDataSource ID="sqlds_affiliates" runat="server"  
                ConnectionString="<%$ ConnectionStrings:Master_ConnectionString %>"  
                SelectCommand="SELECT [source_id], (CAST([source_id] AS VarChar) + '  ' + [affiliate_name]) AS AffiliateName FROM [lead_affiliates] ORDER BY [source_id]"
            </asp:SqlDataSource> 
<telerik:RadTreeView ID="tree_banned_sources" Runat="server"  
                AppendDataBoundItems="True" CheckBoxes="True"  
                DataSourceID="sqlds_affiliates" DataTextField="AffiliateName"  
                DataValueField="source_id" MultipleSelect="True"
                <CollapseAnimation Duration="100" Type="OutQuint" /> 
                <ExpandAnimation Duration="100" /> 
            </telerik:RadTreeView> 
        </td> 

6 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 04 Feb 2009, 12:14 PM
Hi Shawn,

You can access the bound column value in the edit mode and then the Check the tree nodes as shown below.

CS:
  protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
       if((e.Item is GridEditFormItem)&&(e.Item.IsInEditMode)) 
       { 
           GridEditFormItem editFormitem = (GridEditFormItem)e.Item; 
 
           //to access the TreeView from FormTemplate 
           RadTreeView RadTree = (RadTreeView)editFormitem.FindControl("RadTreeView1"); 
 
           //to access the parent Item 
           GridDataItem dataItem = (GridDataItem)editFormitem.ParentItem; 
           string strtxt = dataItem["QuantityPerUnit"].Text.ToString(); 
            
           string[] strArr; 
           // to store in a string array 
           strArr = strtxt.Split(','); 
            
           //loop through string array 
           for (int j = 0; j < strArr.Length; j++) 
           { 
             foreach(RadTreeNode node in RadTree.Nodes ) 
             { 
 
              // loop through each node and then check the nodes accordingly  
 
                 if (node.Index.ToString() == strArr[j].ToString()) 
                 { 
                     node.Checked = true
                 } 
             } 
           } 
 
       } 
    } 


Thanks
Shinu




0
Shawn
Top achievements
Rank 1
answered on 04 Feb 2009, 05:56 PM
Below is the solution in VB, I got it work thank you...

But now I have another issue.  When I have the code below active, I can not insert a new record. The empty form doesn't even become visible and I get this error in IE:

A Runtime Error Has Occurred.
Do you wish to Debug?

Line: 4723
Error: Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.

Any idea why this is happening?

VB.net Solution to TreeView Array:
Protected Sub grid_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grid.ItemDataBound 
 
        If (TypeOf e.Item Is GridEditFormItem) And e.Item.IsInEditMode Then 
 
            Dim editFormItem As GridEditFormItem = TryCast(e.Item, GridEditFormItem) 
 
            'accessing banned sources treeview 
            Dim BannedSourcesTree As RadTreeView = CType(editFormItem.FindControl("tree_banned_sources"), RadTreeView) 
 
            'accessing parent item 
            Dim dataItem As GridDataItem = editFormItem.ParentItem 
            Dim banned_sources_STR As String = dataItem("banned_sources").Text.ToString() 
 
            Dim banned_sources_ARRAY As Array = Split(banned_sources_STR, ","
 
            'loop through array 
            Dim x As Integer 
            For x = 0 To (banned_sources_ARRAY.Length - 1) 
 
                For Each node As RadTreeNode In BannedSourcesTree.Nodes 
 
                    If node.Value.ToString = banned_sources_ARRAY(x).ToString().Trim Then node.Checked = True 
 
                Next 
 
            Next x 
 
        End If 
 
    End Sub 




0
Accepted
Shinu
Top achievements
Rank 2
answered on 05 Feb 2009, 10:14 AM
Hi Shawn,

Give a try with the following approach and see whether it is working.

VB:
Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) 
     
     If (TypeOf e.Item Is GridEditFormItem) AndAlso (e.Item.IsInEditMode) AndAlso (Not e.Item.OwnerTableView.IsItemInserted) Then 
         
         
         Dim editFormItem As GridEditFormItem = TryCast(e.Item, GridEditFormItem) 
         
         'accessing banned sources treeview 
         Dim BannedSourcesTree As RadTreeView = DirectCast(editFormItem.FindControl("tree_banned_sources"), RadTreeView) 
         
         'accessing parent item 
         Dim dataItem As GridDataItem = editFormItem.ParentItem 
         Dim banned_sources_STR As String = dataItem("banned_sources").Text.ToString() 
         
         Dim banned_sources_ARRAY As Array = Strings.Split(banned_sources_STR, ","
         
         'loop through array 
         Dim x As Integer = 0 
         For x = 0 To (banned_sources_ARRAY.Length - 1) 
             
             For Each node As RadTreeNode In BannedSourcesTree.Nodes 
                 
                 If node.Value.ToString = banned_sources_ARRAY(x).ToString().Trim Then 
                     node.Checked = True 
                 End If 
             Next 
             
         Next 
         
     End If 
     
     
 End Sub 

Thanks
Shinu

0
Shawn
Top achievements
Rank 1
answered on 05 Feb 2009, 01:57 PM
That's it - thank you!
0
Shawn
Top achievements
Rank 1
answered on 05 Feb 2009, 11:20 PM
How do I get the value of checked nodes in a grid's UpdateCommand?

I want the values to be in a string like: "5,61,64,91,137,139,161"

Dim banned_sources As String = (CType(editedItem.FindControl("tree_banned_sources"), RadTreeView)).SelectedNodes.ToString() 

0
Accepted
Shinu
Top achievements
Rank 2
answered on 06 Feb 2009, 05:58 AM
Hi Shawn,

Here is the code snippet that performs the desired operation.

VB:
 
Protected Sub RadGrid1_UpdateCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) 
    Dim radTree As RadTreeView = DirectCast(e.Item.FindControl("RadTreeView1"), RadTreeView) 
    Dim nodeVal As String = String.Empty 
     
    For Each node As RadTreeNode In radTree.Nodes 
        If node.Checked Then 
            nodeVal += node.Index.ToString() & "," 
             
        End If 
         
    Next 
     
    Dim newNodeVal As String = nodeVal.TrimEnd(","c) 
    'Perform the update operation here 
     
End Sub 
 


Thanks
Shinu




Tags
Grid
Asked by
Shawn
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Shawn
Top achievements
Rank 1
Share this question
or