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

can't get values in radgrid formtemplate that were not in header

1 Answer 35 Views
Grid
This is a migrated thread and some comments may be shown as answers.
T
Top achievements
Rank 1
T asked on 04 May 2012, 09:38 PM
Hello!
I've been searching for many eons it seems, to get a solution to something that shouldn't be that hard.  i have a radgrid with a formtemplate that saves nicely.  On display in the radgrid header are some fields useful to the user but not all data is in the header.  on editing a row, there are listboxes that need to bind to the proper id - this is my issue - they don't bind by themselves on edit.  Again, the id's for these listboxes are not in the header.

I'm showing a sample of the form here.  (i've noticed there are a lot of uses of "Itemid" as the key to the grid and the listbox, but that shouldn't be an issue since they use different data sources.)
<telerik:RadGrid ID="RadGridPlantedPlants" runat="server"  >
<PagerStyle Mode="NumericPages" AlwaysVisible="true" />
 
<MasterTableView CommandItemDisplay="TopAndBottom" DataKeyNames="ItemId"  >
<Columns>
            
    <telerik:GridBoundColumn UniqueName="anItem" HeaderText="ItemId" DataField="ItemId">
        <HeaderStyle Width="60px"></HeaderStyle>
    </telerik:GridBoundColumn>
     
    <telerik:GridBoundColumn UniqueName="uName" HeaderText="name" DataField="name">
    </telerik:GridBoundColumn>
     
</Columns>
 
<EditFormSettings InsertCaption="Add new item" CaptionFormatString="Edit ItemId: {0}"
    CaptionDataField="ItemId" EditFormType="Template" PopUpSettings-Modal="true"
     FormTableItemStyle-HorizontalAlign="Justify">
     
    <FormTemplate>
     
                 
      <table id="TableLayout"  cellspacing="3" cellpadding="5" width="340" border="0">
    
        <tr>
            <td>
                Location:<br />(Largest geographical area)<br />
                <telerik:RadComboBox ID="plantingLocationsCombo" runat="server"       
                    DataTextField="name"
                    DataValueField="ItemId"
                    DataSourceID="Objectdata_plantingLocations"
                    Width="200px">
                </telerik:RadComboBox>
             
        </tr>
         
    </table>    
        
    </FormTemplate>
</EditFormSettings>
</MasterTableView>
<ClientSettings>
<ClientEvents OnRowDblClick="RowDblClick" />
</ClientSettings>
</telerik:RadGrid>

So, of the many examples i've come across, this below seems to be the closest to getting what i need done.  It works fine when there is no formtemplate.  Most of the pieces are in place, but i just can't get my hands on the data in order to tie the listboxes to the data. 

What is really frustrating, is that editformitem.DataItem is available in the debugger and shows all the values from the database but I cannot get my hands on them.

I've seen examples making use of GridDataKeyArray DataKeyValues , parentTables, DataRow[] and hashtable but something is just not clicking.  The example here crashes on examining the GetDataKeyValue.

protected void RadGridPlantedPlants_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
{
GridEditFormItem editFormItem = e.Item as GridEditFormItem;
//editFormItem.DataItem in debugger shows everything - but I can't get a handle to it
RadComboBox plantingLocationsCombo = (RadComboBox)editFormItem.FindControl("plantingLocationsCombo");
if (editFormItem.ItemIndex != -1) //when = -1 is a new item
{
plantingLocationsCombo.SelectedValue = editFormItem.GetDataKeyValue("pcat_tbllu_location_id").ToString();
}
}
}


Someone's help is greatly appreciated!!!

1 Answer, 1 is accepted

Sort by
0
T
Top achievements
Rank 1
answered on 05 May 2012, 03:14 AM
...nothing like making a post to finally figure something out...
I finally got something to work. Ok, i'll admit this soulution is not elegant, lame-o in fact, but it works.

park this in the <formtemplate>
<asp:HiddenField ID="pcat_tbllu_location_id" runat="server"  Value='<%# Bind( "pcat_tbllu_location_id") %>'/>               


do this in ItemDataBound (not itemcreated)

works for me ..

Anyone with a more elegant solution that takes advantage of what seems to be a built in mechanism, I'd love to see it.
protected void RadGridPlantedPlants_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
 {
  GridEditFormItem editForm = (GridEditFormItem)e.Item;
  RadComboBox plantingLocationsCombo = (RadComboBox)editForm.FindControl("plantingLocationsCombo");
  HiddenField pid = (HiddenField)editForm.FindControl("pcat_tbllu_location_id");
  plantingLocationsCombo.SelectedValue = pid.Value; 
  }
}


thanks for reading this.

Tags
Grid
Asked by
T
Top achievements
Rank 1
Answers by
T
Top achievements
Rank 1
Share this question
or