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

GridEditableItem UpdateValues doesn't work with GridTemplateColumn?

3 Answers 312 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Margo Noreen
Top achievements
Rank 1
Veteran
Margo Noreen asked on 13 Apr 2010, 07:34 PM
I have a page with a RadGrid on it that I've been testing with.  I do not have any kind of DataSource control on it, so I am manually wiring up a datasource to it on the NeedDataEvent and then, of course, wiring up the various command item events.  I had my test page all working with insert, update, and delete events until... I changed several columns from simple GridBoundColumn columns to GridTemplateColumn columns.

Using the GridBoundColumn type, the following code worked just fine:

    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)  
    {  
        DataTable dt = GridSource;  
        GridEditableItem eeditItem = e.Item as GridEditableItem;  
 
        int rowkey = (int?)editItem.GetDataKeyValue(IDColumnName) ?? 0;  
 
        DataRow row = dt.Rows.Find(rowkey);  
        if (row != null)  
        {  
            row.BeginEdit();  
            try  
            {  
                editItem.UpdateValues(row);  
                row["EditAction"] = "update";  
                row.EndEdit();  
 
                TrackChange(rowkey, "update");  
            }  
            catch (Exception ex)  
            {  
                row.CancelEdit();  
                RadGrid1.Controls.Add(new LiteralControl("Unable to update record: " + ex.Message));  
                e.Canceled = true;  
            }  
        }  
        txtCommandInfo.Text = String.Format("ItemIndex {0} rowkey {1}", e.Item.ItemIndex.ToString(), rowkey);  
    }  
 

The code line of interest is inside the try block where it calls the editItem.UpdateValues method.  And it is this line that no longer seems to work if my columns are of the GridTemplateColumn type.  There is is no error that is thrown, it's just the row (a DataRow object) is not updated with new values from any template columns, only bound columns.

In both cases, I have the MasterTableView setup with "EditMode="EditForms", the only change is in the markup. 

for the bound column, I had this:

                    <telerik:GridBoundColumn HeaderText="FirstName" DataField="FirstName" UniqueName="FirstName" 
                        MaxLength="20" /> 
 

for the template column, I had this:
                    <telerik:GridTemplateColumn HeaderText="FirstName" UniqueName="FirstNameTemplate">  
                        <ItemTemplate> 
                            <%# Eval("FirstName") %> 
                        </ItemTemplate> 
                        <EditItemTemplate> 
                            <asp:TextBox ID="FirstName" runat="server" Text='<%# Eval("FirstName") %>' /> 
                            <asp:RequiredFieldValidator ID="rfvFirstName" runat="server" ErrorMessage="First name is required." 
                                ControlToValidate="FirstName"></asp:RequiredFieldValidator> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
 

So when changing to the template column, I find that I can code something like
        
 
        string txt;  
        TextBox ctrl = (TextBox)editItem.FindControl("FirstName");  
        if (ctrl != null)  
            txt = ctrl.Text;  
 

and of course work on manually updating my DataRow object accordingly.  But... I'm trying to *understand* this, and quite frankly I'm not finding much documentation on it. 

So... could someone provide me pointers to documentation on the differences between Template and Bound columns?  Or even some more reference material on the ExtractValues, UpdateValues methods?

I realize I can work on coding up a scenario where I use the .FindControl method for every column I'm interested in, but I guess I'm after a little more education...

Any help would be greatly appreciated.  Thanks in advance.

3 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 16 Apr 2010, 07:13 AM
Hello Margo,

You in order to extract values from a template you should use two-way binding through Bind instead of one-way (Eval). Thus you textbox should look like the following:

<asp:TextBox ID="FirstName" runat="server" Text='<%# Bind("FirstName") %>' />


Kind regards,
Rosen
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Aleksander
Top achievements
Rank 1
answered on 02 Mar 2012, 08:31 PM
I can't get the bind to work with the value on the radnumerictextbox..

here is what i have.

<tk:GridTemplateColumn HeaderText="Threshold Number" DataField="ThresholdNumber" UniqueName="ThresholdNumber" SortExpression="ThresholdNumber" ColumnEditorID="ThresholdNumberEditor" HeaderStyle-Width="150px" >
                                <ItemTemplate>
                                    <%# Eval("ThresholdNumber") %>
                                </ItemTemplate>
                                <InsertItemTemplate>
                                    <tk:RadNumericTextBox ID="tbThresholdNumber" runat="server" Value='<%# Convert.ToDecimal(Bind("ThresholdNumber")) %>' Width="50px" ShowSpinButtons="true" NumberFormat-DecimalDigits="0" />
                                </InsertItemTemplate>
                                <EditItemTemplate>
                                    <tk:RadNumericTextBox ID="tbThresholdNumber" runat="server" Value='<%# Convert.ToDecimal(Eval("ThresholdNumber")) %>' Width="50px" ShowSpinButtons="true" NumberFormat-DecimalDigits="0" />
                                </EditItemTemplate>
                            </tk:GridTemplateColumn>
0
Jayesh Goyani
Top achievements
Rank 2
answered on 03 Mar 2012, 07:08 AM
Hello Aleksander,

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false"
           OnNeedDataSource="RadGrid1_NeedDataSource"
           onitemdatabound="RadGrid1_ItemDataBound"
           onupdatecommand="RadGrid1_UpdateCommand">
           <MasterTableView CommandItemDisplay="Top">
               <Columns>
               <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
                   <telerik:GridTemplateColumn HeaderText="Threshold Number" DataField="ThresholdNumber"
                       UniqueName="ThresholdNumber" SortExpression="ThresholdNumber" ColumnEditorID="ThresholdNumberEditor"
                       HeaderStyle-Width="150px">
                       <ItemTemplate>
                           <%# Eval("ThresholdNumber")%>
                       </ItemTemplate>
                       <InsertItemTemplate>
                           <telerik:RadNumericTextBox ID="tbThresholdNumber" runat="server" Value='<%# Convert.ToDecimal(Eval("ThresholdNumber")) %>'
                               Width="50px" ShowSpinButtons="true" NumberFormat-DecimalDigits="0" />
                       </InsertItemTemplate>
                       <EditItemTemplate>
                           <telerik:RadNumericTextBox ID="tbThresholdNumber" runat="server" Value='<%# Convert.ToDecimal(Eval("ThresholdNumber")) %>'
                               Width="50px" ShowSpinButtons="true" NumberFormat-DecimalDigits="0" />
                       </EditItemTemplate>
                   </telerik:GridTemplateColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       DataTable dt = new DataTable();
       dt.Columns.Add("ThresholdNumber", typeof(decimal));
       dt.Rows.Add(1.1);
       RadGrid1.DataSource = dt;
   }
   protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridEditableItem)
       {
           GridEditableItem item = e.Item as GridEditableItem;
           RadNumericTextBox tbThresholdNumber = item.FindControl("tbThresholdNumber") as RadNumericTextBox;
       }
   }
   protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
   {
       GridEditableItem item = e.Item as GridEditableItem;
       RadNumericTextBox tbThresholdNumber = item.FindControl("tbThresholdNumber") as RadNumericTextBox;
       Response.Write(tbThresholdNumber.Value);
   }


Thanks
Jayesh Goyani
Tags
Grid
Asked by
Margo Noreen
Top achievements
Rank 1
Veteran
Answers by
Rosen
Telerik team
Aleksander
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or