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

Updating Cell on the Fly

2 Answers 24 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 30 Jul 2012, 04:25 AM
I've created a grid where the last column is defined thusly:

      <telerik:GridTemplateColumn UniqueName="StdHrs" DataField="StdHrs">
        <HeaderTemplate>Std Hrs</HeaderTemplate>
        <ItemTemplate>
          <asp:Label ID="labelStdHrs" runat="server" Width="100%" Text='<%# Bind("StdHrs") %>' />
        </ItemTemplate>            
      </telerik:GridTemplateColumn> 

The Std Hrs on each row = Quantity x Rate

After a little research I determined this approach to update the Std Hrs value when a number is changed in the Quantity (aka Units) TextBox:

          float stdHrs = Tools.Round((float)dataRows[0]["Rate"] * newval, 2);
          dataRows[0]["StdHrs"] = stdHrs;
          GridDataItem item = (GridDataItem)textBox.NamingContainer;
          Label label = (Label)item.FindControl("labelStdHrs");
          label.Text = stdHrs.ToString();


This does work but it seems rather redundant to have to update both the underlying DataTable (that the Grid is bound to) as well as the Label that sits in the row.

Is there, in fact, a more efficient way to do this?

Robert

2 Answers, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 02 Aug 2012, 08:52 AM
Hello Robert,

The grid is control that shows data from the database in some particular design. If you like to show different data, it should come from the database. Exception can be made if you use Eval to evaluate the value from the label from your custom function. This function can be defined inside your Page, and to keep the values for each label in the session, to avoid calling the database each time.
You can check this web site with example of something similar.

Greetings,
Vasil
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Robert
Top achievements
Rank 1
answered on 03 Aug 2012, 04:54 AM
Vasil,

After reading your link, I tried using just the first two lines of my code, without the remaining three.  In other words, just this remained:

          float stdHrs = Tools.Round((float)dataRows[0]["Rate"] * newval, 2);
          dataRows[0]["StdHrs"] = stdHrs;

Unfortunately (and not unexpectedly) that didn't automatically update the StdHrs column on the grid.

So then I tried to rebind the grid to the DataTable again.  That caused problems elsewhere in my code.

Thus, I've just left things as-is, with the original 5 lines of code.  Though it clearly isn't the most elegant solution, it does work.

Robert
Tags
Grid
Asked by
Robert
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Robert
Top achievements
Rank 1
Share this question
or