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

How to set the value of a control after Update

11 Answers 203 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ida
Top achievements
Rank 1
Ida asked on 05 Nov 2013, 05:24 PM
Hi,
I have a header control in my RadGrid set out as:

<

 

 

telerik:GridBoundColumn DataField="RaisingFactor" HeaderText="Raising Factor" UniqueName="RaisingFactor" ReadOnly="true" Visible="true" DataFormatString="{0:N2}"></telerik:GridBoundColumn>

 


I wish to re-set the value of the text of this control after the Update has been made on the row that it belongs to.

Its a field that is calculated based on the values of 3 other fields. I wish the new value of this header to display straight after Update.

Any advice?

Thank you.

11 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Nov 2013, 05:43 AM
Hi Ida,

I'm not clear about your requirement, I guess you want to change the value of a column to the sum of the three column of a row after update.
Try the following code snippet. If this doesn't help, please elaborate on your requirements.

C#:
bool isUpdate = false;
int value;
  
protected void RadGrid_Master_UpdateCommand(object sender, GridCommandEventArgs e)
{
    isUpdate = true;
    GridEditableItem edit = (GridEditableItem)e.Item;
    value = Convert.ToInt32(edit.GetDataKeyValue("ID"));
   //Code to Update
}
protected void RadGrid_Master_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem && isUpdate)
    {
        GridDataItem item = (GridDataItem)e.Item;
        int id = Convert.ToInt32(item.GetDataKeyValue("ID")); // Accessing the row using Datakeyvalue
        if (value == id) // Checking if its the Updated Row
        {
            int column1 = Convert.ToInt32(item["ColumnUniqueName1"].Text); //Accessing the 1st Column's value
            int column2 = Convert.ToInt32(item["ColumnUniqueName2"].Text); //Accessing the 2nd Column's value
            int column3 = Convert.ToInt32(item["ColumnUniqueName3"].Text); //Accessing the 3rd Column's value
            int totalValue = column1 + column2 + column3;
            item["RaisingFactor"].Text = totalValue.ToString(); // Set the total
        }
    }
}


Thanks,
Princy
0
Ida
Top achievements
Rank 1
answered on 06 Nov 2013, 10:06 AM
Hi Princy,
Hopefully my VB UpdateCommand code below will explain better what I am trying to do.

I click the Edit button. Make changes to 2 textboxes of numerical values. Then I click the Update button. 

The header control called RaisingFactor is not updated to the new calculated value it should be AFTER the Update button is clicked. It eventually updates to new value if I click the Edit button again.

When I debug through the code the value for editorRaisingFactor.Text   is correct, though I want to know how to get the new updated value to display on the screen in the control after Update.

Private Sub RDCatchTypes_UpdateCommand(source As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RDCatchTypes.UpdateCommand
           ' Clear any error messages off the screen
           system_error.Visible = False
           Try
               If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
                   Dim edit As GridEditableItem = DirectCast(e.Item, GridEditableItem)
                   ' List of parameter values that need to be passed into the UPDATE SQL query
                   TripCode = RTrim(CStr(ddlTripCode.SelectedItem.Value))
                   Dim VesselCode As String = RTrim(TryCast(edit("VesselCode").Controls(0), TextBox).Text)
                   Dim HaulNumber As Integer = CInt(TryCast(edit("HaulNumber").Controls(0), TextBox).Text)
                   Dim RigNumber As Integer = CInt(TryCast(edit("RigNumber").Controls(0), TextBox).Text)
                   Dim CatchTypeCode As Integer = CInt(TryCast(edit("CatchTypeCode").Controls(0), TextBox).Text)
                   Dim BasketsCaughtTotal As Decimal = CDec(TryCast(edit("BasketsCaughtTotal").Controls(0), TextBox).Text)
                   Dim BasketsSampledTotal As Decimal = CDec(TryCast(edit("BasketsSampledTotal").Controls(0), TextBox).Text)
                   Dim ProportionSubSampled As Decimal = CDec(TryCast(edit("ProportionSubSampled").Controls(0), TextBox).Text)
                   ' Update the Catch Type details, using the control values as parameters
                   Dim daUpdateCatchType As New DSRaisingFactorsTableAdapters.DisGetCatchTypeDataByTripcodeTableAdapter
                   daUpdateCatchType.DisUpdateCatchTypeDetails(TripCode, VesselCode, HaulNumber, RigNumber, CatchTypeCode, BasketsCaughtTotal, BasketsSampledTotal, ProportionSubSampled)
                   ' Update the Raising Factor shown in the grid header
                   Dim editItemRaisingFactor As GridEditableItem = DirectCast(e.Item, GridEditableItem)
                   Dim editManagerRaisingFactor As GridEditManager = editItemRaisingFactor.EditManager
                   Dim editorRaisingFactor As GridTextBoxColumnEditor = DirectCast(editItemRaisingFactor.EditManager.GetColumnEditor("RaisingFactor"), GridTextBoxColumnEditor)
                   Dim dsRaisingFactor As New DSRaisingFactorsTableAdapters.DisGetCatchTypeDataByTripcodeTableAdapter
                   editorRaisingFactor.Text = daUpdateCatchType.DisGetRaisingFactorByTripHaulRigCatch(TripCode, VesselCode, HaulNumber, RigNumber, CatchTypeCode)
               End If
           Catch ex As Exception
               e.Canceled = True
               errorRaised = True
               errorMessage = ex.Message
               errorMessage = "There was a problem updating the raising factor(s). Please contact the administrator of the system to report the problem: " & ex.Message
               If errorRaised = True Then
                   system_error.Visible = True
                   lblError.Text = errorMessage
               End If
           End Try
       End Sub
0
Princy
Top achievements
Rank 2
answered on 07 Nov 2013, 06:34 AM
Hi Ida,

I suppose you are setting the RaisingFactor column as ReadOnly="true". When a column is readonly, it's not visible in edit mode. Hence you can't set its Text in UpdateCommand as it doesn't affect it in view-mode. Since you are not updating the value of RaisingFactor to DB, its changes cannot be reflected in view-mode when doing calculations in the UpdateCommand. Hence if you want to set the Text of the column in view-mode its advisable to code in the ItemDataBound event of the RadGrid.
Hope this helps, let me know if any concern.


Thanks,
Princy
0
Ida
Top achievements
Rank 1
answered on 07 Nov 2013, 11:06 AM
Hi Princy

Thank you for the advice.

I understand what you mean about the fact that my RaisingFactor column is not editable.

The ItemDataBound did not seem to work for me. So I tried an alternative way of updating this column by using a GridCalculatedColumn. But, the RaisingFactor still does not show the newly calculated value until I click the Edit button again. Any advice please?

<telerik:RadGrid ID="RDCatchTypes" Width="530" runat="server" AutoGenerateEditColumn="True" GridLines="Horizontal" AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#ecf3f4" BorderColor="#28899A" EditItemStyle-BackColor="#009999" EditItemStyle-Font-Bold="true" OnNeedDataSource="RDCatchTypes_NeedDataSource">
          <AlternatingItemStyle BackColor="#ECF3F4"></AlternatingItemStyle>
          <HeaderStyle BorderColor="#28899A" Font-Bold="True" Font-Size="12px" Font-Names="Verdana" />
             <MasterTableView EnableViewState="false">
                   <Columns>
                      <telerik:GridBoundColumn DataField="TripCode" FilterControlAltText="Filter TripCode column" HeaderText="Trip Code" SortExpression="TripCode" UniqueName="TripCode" ReadOnly="true"></telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="VesselCode" HeaderText="Vessel" UniqueName="VesselCode" Visible="false" ReadOnly="true"></telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="HaulNumber" HeaderText="Haul" UniqueName="HaulNumber" ReadOnly="true"></telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="RigNumber" HeaderText="Rig"  UniqueName="RigNumber" ReadOnly="true"></telerik:GridBoundColumn>    
                      <telerik:GridBoundColumn DataField="CatchTypeName" HeaderText="Catch Type" UniqueName="CatchTypeName" ReadOnly="true"></telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="CatchTypeCode" HeaderText="Catch Type Code" Visible="false" UniqueName="CatchTypeCode" ReadOnly="true"></telerik:GridBoundColumn>
                        
                      
  
                      <telerik:GridCalculatedColumn HeaderText="Raising Factor" UniqueName="RaisingFactor" DataType="System.Decimal" DataFormatString="{0:N2}" DataFields="BasketsCaughtTotal, BasketsSampledTotal, ProportionSubsampled" Expression="{0}/{1}/{2}" />
  
                        
                        
                      <telerik:GridBoundColumn DataField="BasketsCaughtTotal" HeaderText="Baskets Caught Total" UniqueName="BasketsCaughtTotal" ColumnEditorID="BasketsCaughtTotal" Visible="false" DataType="System.Decimal" DataFormatString="{0:N5}">
                           <ColumnValidationSettings EnableRequiredFieldValidation="True">
                                <RequiredFieldValidator ID="RFTotalBasketsCaught" Font-Bold="True"> Enter baskets caught</RequiredFieldValidator>
                            </ColumnValidationSettings>
                      </telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="BasketsSampledTotal" HeaderText="Baskets Sampled Total" UniqueName="BasketsSampledTotal" ColumnEditorID="BasketsSampledTotal" Visible="false" DataType="System.Decimal" DataFormatString="{0:N5}">
                           <ColumnValidationSettings EnableRequiredFieldValidation="True">
                                <RequiredFieldValidator ID="RFTotalBasketsSampled" Font-Bold="True"> Enter baskets sampled</RequiredFieldValidator>
                            </ColumnValidationSettings>
                      </telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="ProportionSubsampled" HeaderText="Proportion Sub-Sampled" UniqueName="ProportionSubSampled" ReadOnly="true" Visible="false"></telerik:GridBoundColumn>
                    </Columns>
                  </MasterTableView>
              </telerik:RadGrid>


0
Princy
Top achievements
Rank 2
answered on 08 Nov 2013, 04:36 AM
Hi Ida,

I see that you have set the columns ReadOnly property to true, doing that the columns will not be editable. Then you have set the Visible="false", in such case these controls will not be accessible in the code behind. Make sure that all columns needed for Update have Visibility as True and are not ReadOnly. Please set EnableViewState="true" and see if it helps. Below is the code that i tried and works fine at my end.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateEditColumn="True"
    AutoGenerateColumns="false" DataSourceID="SqlDataSource1" OnUpdateCommand="RadGrid1_UpdateCommand">  
    <MasterTableView EnableViewState="true" DataKeyNames="OrderID">
        <Columns>
            <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" UniqueName="OrderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" UniqueName="EmployeeID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ShipVia" HeaderText="ShipVia" UniqueName="ShipVia">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Freight" HeaderText="Freight" UniqueName="Freight">
            </telerik:GridBoundColumn>
            <telerik:GridCalculatedColumn HeaderText="Raising Factor" UniqueName="RaisingFactor"
                DataType="System.Decimal" DataFormatString="{0:N2}" DataFields="EmployeeID, ShipVia, Freight"
                Expression="{0}/{1}/{2}" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

VB:
Protected Sub RadGrid1_UpdateCommand(sender As Object, e As GridCommandEventArgs)
    Dim edit As GridEditableItem = DirectCast(e.Item, GridEditableItem)
    Dim OrderID As String = edit.GetDataKeyValue("OrderID").ToString()
    Try
        Dim id As String = TryCast(edit("EmployeeID").Controls(0), TextBox).Text
        Dim shipvia As String = TryCast(edit("ShipVia").Controls(0), TextBox).Text
        Dim freightnum As String = TryCast(edit("Freight").Controls(0), TextBox).Text
 
        'Code to Update
         
    Catch ex As Exception
    End Try
End Sub

Thanks,
Princy
0
Ida
Top achievements
Rank 1
answered on 08 Nov 2013, 10:28 AM
Hi Princy, thank you for the advice.

Unfortunately when I set EnableViewState="true", none of my data displayed. No data was extracted. So I seemed to be unable to have that setting?

The 3 values that I need for calculating the update of my RaisingFactor control, I set Visibility as True and are not ReadOnly . This did not make a difference I'm afraid.

The RaisingFactor column did not display the correct new value once I clicked Update. The correct new value only appeared after I clicked the Edit link again. The RaisingFactor column only seems to refresh its value after a second click of the Edit link.

I'm totally baffled as to why this is happening as the grid does not need to make another call to the database.

Any advice?

Thank you, Ida



0
Princy
Top achievements
Rank 2
answered on 11 Nov 2013, 10:47 AM
Hi Ida,

Unfortunately I couldn't replicate the issue. When you are using AdvancedDataBinding, the grid will automatically refresh after insert/update operation. Make sure that you have not set AllowAutomaticUpdates as true.

Thanks,
Princy
0
Ida
Top achievements
Rank 1
answered on 13 Nov 2013, 09:25 AM
Hi Princy,

Hmmm...will just have to keep trying different things. I have AllowAutomaticUpdates set to False, though unfortunately the column I need re-calculated does not automatically update.

Thank you for your help!

Ida
0
Kostadin
Telerik team
answered on 18 Nov 2013, 07:52 AM
Hello Ida,

Could you please provide your code declaration and the related code behind in order to investigate the issue further? I assume there is a binding issue which prevents the updating of the column values. I am looking forward to your reply.

Regards,
Kostadin
Telerik
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 the blog feed now.
0
Ida
Top achievements
Rank 1
answered on 18 Nov 2013, 10:15 AM
Hi Kostadin,
Here is my grid:

<telerik:RadGrid ID="RDCatchTypes" Width="530" runat="server" AutoGenerateEditColumn="True" AllowAutomaticUpdates="false" GridLines="Horizontal" AutoGenerateColumns="false" AlternatingItemStyle-BackColor="#ecf3f4" BorderColor="#28899A" EditItemStyle-BackColor="#009999" EditItemStyle-Font-Bold="true" OnNeedDataSource="RDCatchTypes_NeedDataSource" ClientSettings-EnablePostBackOnRowClick="true">
          <AlternatingItemStyle BackColor="#ECF3F4"></AlternatingItemStyle>
          <ClientSettings AllowKeyboardNavigation="true" EnablePostBackOnRowClick="true"
            <Selecting AllowRowSelect="true"></Selecting
          </ClientSettings
  
          <HeaderStyle BorderColor="#28899A" Font-Bold="True" Font-Size="12px" Font-Names="Verdana" />
             <MasterTableView EnableViewState="false">
                   <Columns>
                      <telerik:GridBoundColumn DataField="TripCode" FilterControlAltText="Filter TripCode column" HeaderText="Trip Code" SortExpression="TripCode" UniqueName="TripCode" ReadOnly="true"></telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="VesselCode" HeaderText="Vessel" UniqueName="VesselCode" Visible="false" ReadOnly="true"></telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="HaulNumber" HeaderText="Haul" UniqueName="HaulNumber" ReadOnly="true"></telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="RigNumber" HeaderText="Rig"  UniqueName="RigNumber" ReadOnly="true"></telerik:GridBoundColumn>    
                      <telerik:GridBoundColumn DataField="CatchTypeName" HeaderText="Catch Type" UniqueName="CatchTypeName" ReadOnly="true"></telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="CatchTypeCode" HeaderText="Catch Type Code" Visible="false" UniqueName="CatchTypeCode" ReadOnly="true"></telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="RaisingFactor" HeaderText="Raising Factor" UniqueName="RaisingFactor" ReadOnly="true" Visible="true" DataFormatString="{0:N2}"></telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="BasketsCaughtTotal" HeaderText="Baskets Caught Total" UniqueName="BasketsCaughtTotal" ColumnEditorID="BasketsCaughtTotal" Visible="false" DataType="System.Decimal" DataFormatString="{0:N5}">
                           <ColumnValidationSettings EnableRequiredFieldValidation="True">
                                <RequiredFieldValidator ID="RFTotalBasketsCaught" Font-Bold="True"> Enter baskets caught</RequiredFieldValidator>
                            </ColumnValidationSettings>
                      </telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="BasketsSampledTotal" HeaderText="Baskets Sampled Total" UniqueName="BasketsSampledTotal" ColumnEditorID="BasketsSampledTotal" Visible="false" DataType="System.Decimal" DataFormatString="{0:N5}">
                           <ColumnValidationSettings EnableRequiredFieldValidation="True">
                                <RequiredFieldValidator ID="RFTotalBasketsSampled" Font-Bold="True"> Enter baskets sampled</RequiredFieldValidator>
                            </ColumnValidationSettings>
                      </telerik:GridBoundColumn>
                      <telerik:GridBoundColumn DataField="ProportionSubsampled" HeaderText="Proportion Sub-Sampled" UniqueName="ProportionSubSampled" ColumnEditorID="ProportionSubSampled" ReadOnly="true" Visible="false"></telerik:GridBoundColumn>
                    </Columns>
                  </MasterTableView>
              </telerik:RadGrid>

And I have set a re-bind on the RaisingFactor column once an Update is made and that seems to have fixed the problem:

Private Sub RDCatchTypes_UpdateCommand(source As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RDCatchTypes.UpdateCommand
               If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
                   Dim edit As GridEditableItem = DirectCast(e.Item, GridEditableItem)
                   ' List of parameter values that need to be passed into the UPDATE SQL query
                   TripCode = RTrim(CStr(ddlTripCode.SelectedItem.Value))
                   Dim VesselCode As String = RTrim(TryCast(edit("VesselCode").Controls(0), TextBox).Text)
                   Dim HaulNumber As Integer = CInt(TryCast(edit("HaulNumber").Controls(0), TextBox).Text)
                   Dim RigNumber As Integer = CInt(TryCast(edit("RigNumber").Controls(0), TextBox).Text)
                   Dim CatchTypeCode As Integer = CInt(TryCast(edit("CatchTypeCode").Controls(0), TextBox).Text)
                   Dim BasketsCaughtTotal As Decimal = CDec(TryCast(edit("BasketsCaughtTotal").Controls(0), TextBox).Text)
                   Dim BasketsSampledTotal As Decimal = CDec(TryCast(edit("BasketsSampledTotal").Controls(0), TextBox).Text)
                   Dim ProportionSubSampled As Decimal = CDec(TryCast(edit("ProportionSubSampled").Controls(0), TextBox).Text)
                   Dim RaisingFactor As Decimal = CDec(TryCast(edit("RaisingFactor").Controls(0), TextBox).Text)
                   ' Update the Catch Type details, using the control values as parameters
                   Dim daUpdateCatchType As New DSRaisingFactorsTableAdapters.DisGetCatchTypeDataByTripcodeTableAdapter
                   daUpdateCatchType.DisUpdateCatchTypeDetails(TripCode, VesselCode, HaulNumber, RigNumber, CatchTypeCode, BasketsCaughtTotal, BasketsSampledTotal, ProportionSubSampled, RaisingFactor)
                   ' Re-bind the grid so that the Raising Factor is updated
                   Dim ds As New DSRaisingFactorsTableAdapters.DisGetCatchTypeDataByTripcodeTableAdapter
                   ' Bind the grid to the catch type details
                   RDCatchTypes.DataSource = ds.GetCatchTypeDataByTripcode(TripCode)
       End Sub

0
Kostadin
Telerik team
answered on 20 Nov 2013, 03:34 PM
Hello Ida,

I noticed that you have setting the datasource at the bottom of the page which is not supported scenario when using an advanced data binding. I would recommend you to examine the following code library which demonstrates how to implement update, insert, delete operation in grid. Additionally I would suggest you to use a GridCalculatedColumn which will automatically display the calculated value after updating a record.

Regards,
Kostadin
Telerik
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 the blog feed now.
Tags
Grid
Asked by
Ida
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Ida
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or