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

RadGrid GridTemplateColumn FindControl

4 Answers 535 Views
Grid
This is a migrated thread and some comments may be shown as answers.
DanR
Top achievements
Rank 1
DanR asked on 26 Sep 2010, 05:10 PM
I am testing a RadGrid that uses multiple GridTemplateColumns because I need a multi-line header so inside of each GridTemplateColumn I have HeaderTemplate and an ItemTemplate to define cells with a table for display. This works as required. In my test case I have 6 gridtemplatecolumns that make up one RadGrid. Now I want to test how to make one of these six gridtemplatecolumns Not Visible at PreRender. I have tried this in the declarative (as part of the GridTemplateColumn I set this to Visible=False) and the column is not displayed, just as I want. However, I cannot figure out how to accomplish the same in code behind.

Let's say I have:
    GridTemplateColumn UniqueName="GTC1"
         HeaderTemplate
         ItemTemplate
    GridTemplateColumn UniqueName="GTC2"
         HeaderTemplate
         ItemTemplate
    GridTemplateColumn UniqueName="GTC3"
         HeaderTemplate
         ItemTemplate

Now I want to make "GTC2" Not Visible in code behind. How do I perform a findcontrol on "GTC2" in PreRender event? If I can find "GTC2" then I can set Visible=False. I think I want to perform this findcontrol in PreRender or do I need to access the GridTemplateColumn in a different event to make this work?

I tried this in PreRender:

 

Dim gtc As GridTemplateColumn = TryCast(RadGrid1.FindControl("GTC2"), GridTemplateColumn)

 

gtc.Visible =

 

False

 

 


This gives the message "Type System.Web.Ui.Control cannot be converted to Telerik.Web.Ui.GridTemplateColumn." Maybe I cannot access the GridTemplateColumn in code behind? If not, how might I access the tables that are defined inside of the HeaderTemplate and ItemTemplate?

Thanks in advance for any help you can provide...Dan

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 27 Sep 2010, 04:58 AM
Hello Dan,

Try the following code snippet in PreRender event to hide the GridTemplateColumn.

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
      RadGrid1.MasterTableView.GetColumn("GTC2").Visible = false; // hiding TemplateColumn using ColumnUniqueName
   }

Thanks,
Princy.
0
DanR
Top achievements
Rank 1
answered on 01 Oct 2010, 05:36 AM
Hi Princy:

Thanks for the code snippet. It worked just as I needed. Now, I have another question if you can help.

In my scenario described previously, I have a hierarchical Radgrid (two levels). At the main level, I display a grid, then allow the user to open a nested grid where they can insert records tied to the selected row in the main grid. For the nested grid, I use a popup form to allow insert. The form works fine. However, so the user can confirm where they are during the popup entry, I would like to display some fields from the main grid row as read only (I can set these display fields as textboxes in the form, use findcontrol on the textboxes and insert the text from code behind). Can you suggest a code snippet that would show me how to retrieve the text for a datafield that exists in the main radgrid in the row that is selected so that I can display the field in the popup for insert in the nested table?

Thanks, Dan
0
Princy
Top achievements
Rank 2
answered on 01 Oct 2010, 07:42 AM
Hello Dan,

In ItemDataBound event check whether the Detail grid is in insert mode and access the parentItem. Then get the cell value of parentItem by using the ColumnUniqueName.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted && e.Item.OwnerTableView.Name == "Detail2") // check with name of DetailTable
       {
           GridDataItem parentItem = e.Item.OwnerTableView.ParentItem; //accessing parent item
           string value = parentItem["EmployeeID"].Text; // accessing value from the parent item
       }
   }

Thanks,
Princy.
0
DanR
Top achievements
Rank 1
answered on 03 Oct 2010, 04:44 AM
Hi Princy:

Your suggestion worked again. Thank you. How about another question related to working with hierarchical grids?

I have a radgrid that contains detail table. The user can select a row from the main grid, then the detail table is shown. The user then has the option to add new records to the detail grid, or edit recrods that exist in the detail grid. I provide a popup template to allow the user to insert and edit records in the detail grid. One of the fields in the popup (also displayed in the detail grid) is a checkbox. I want to use client side javascript to check the status of this checkbox field in the popup (based on the checkbox, I may need to make other fields in the popup disabled). I have this working in a regular RadGrid with a popup and my javascript is able to find the checkbox control. However, I do not know how to find the control when it exists in a Master grid, that has a detail grid, that then has a popup. Can you suggest a way to perform a findcontrol in javascript to get a control that is in a popup that is called from a detail grid within a master radgrid? Below is the code for the test checkbox I am using. You will note I am using an onclick event to check the status if the checkbox is changed. However, I need to be able to check the status of the checkbox from other events tied to different input fields when they receive the focus.

<asp:Label ID="lblTest"
       Text="Test Description?"
       runat="server"
       AssociatedControlID="cbTest"
       CssClass="popup_Align_Labels_CB" />
<asp:CheckBox ID="cbTest"
              runat="server"
              onclick="checkValues(this);"
              Checked='<%# Bind("Test_CB") %>' />

Once again, thanks for all your help so far...Dan
Tags
Grid
Asked by
DanR
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
DanR
Top achievements
Rank 1
Share this question
or