1

When editing certain column values in my gridview, other values need to change depending on that value.

For example: https://i.sstatic.net/3hZL6.png

When editing the Truck Diesel column value, the KMPerLiter and RandsPerKM values must be changed according to that value and visa versa. The formulas are as follows:

KMPerLiter = TotaKM/TruckDiesel+TrailerDiesel and RandsPerKM = Amount/TotalKM.

This must be done within the edit column of the gridview.

All these values are then stored in a table in a database.

Here is the ASP.Net html source code:

     UpdateCommand="UPDATE [Loads] SET [loadDate] = @loadDate, [loadDay] = @loadDay, [loadClient] = @loadClient, [loadDriver] = @loadDriver, [loadLoadingPoint1] = @loadLoadingPoint1, [loadLoadingPoint2] = @loadLoadingPoint2, [loadLoadingPoint3] = @loadLoadingPoint3, [loadOffloadingPoint1] = @loadOffloadingPoint1, [loadOffloadingPoint2] = @loadOffloadingPoint2, [loadOffloadingPoint3] = @loadOffloadingPoint3, [loadStops] = @loadStops, [loadNumber] = @loadNumber, [loadPodNumber] = @loadPodNumber, [loadAmount] = @loadAmount, [loadTruckReg] = @loadTruckReg, [loadTrailerReg] = @loadTrailerReg, [loadCategory] = @loadCategory, [loadDayOut] = @loadDayOut, [loadNightOut] = @loadNightOut, [loadOdoStart] = @loadOdoStart, [loadOdoEnd] = @loadOdoEnd, [loadTruckDiesel] = @loadTruckDiesel, [loadTrailerDiesel] = @loadTrailerDiesel, [loadInvoiceNumber] = @loadInvoiceNumber, [loadReceiptNumber] = @loadReceiptNumber, [loadKMperLiter] = @loadKMperLiter, [loadRandsPerKM] = @loadRandsPerKM, [loadDriverWage] = @loadDriverWage, [clientRecieved] = @clientRecieved, [loadTotalKM] = @loadTotalKM, [note] = @note WHERE [loadID] = @loadID">
    <UpdateParameters>
        <asp:Parameter DbType="Date" Name="loadDate" />
        <asp:Parameter Name="loadDay" Type="String" />
        <asp:Parameter Name="loadClient" Type="String" />
        <asp:Parameter Name="loadDriver" Type="String" />
        <asp:Parameter Name="loadLoadingPoint1" Type="String" />
        <asp:Parameter Name="loadLoadingPoint2" Type="String" />
        <asp:Parameter Name="loadLoadingPoint3" Type="String" />
        <asp:Parameter Name="loadOffloadingPoint1" Type="String" />
        <asp:Parameter Name="loadOffloadingPoint2" Type="String" />
        <asp:Parameter Name="loadOffloadingPoint3" Type="String" />
        <asp:Parameter Name="loadStops" Type="Int32" />
        <asp:Parameter Name="loadNumber" Type="String" />
        <asp:Parameter Name="loadPodNumber" Type="String" />
        <asp:Parameter Name="loadAmount" Type="Decimal" />
        <asp:Parameter Name="loadTruckReg" Type="String" />
        <asp:Parameter Name="loadTrailerReg" Type="String" />
        <asp:Parameter Name="loadCategory" Type="String" />
        <asp:Parameter Name="loadDayOut" Type="Int32" />
        <asp:Parameter Name="loadNightOut" Type="Int32" />
        <asp:Parameter Name="loadOdoStart" Type="Int32" />
        <asp:Parameter Name="loadOdoEnd" Type="Int32" />
        <asp:Parameter Name="loadTruckDiesel" Type="Decimal" />
        <asp:Parameter Name="loadTrailerDiesel" Type="Decimal" />
        <asp:Parameter Name="loadInvoiceNumber" Type="String" />
        <asp:Parameter Name="loadReceiptNumber" Type="String" />
        <asp:Parameter Name="loadKMperLiter" Type="Decimal" />
        <asp:Parameter Name="loadRandsPerKM" Type="Decimal" />
        <asp:Parameter Name="loadDriverWage" Type="Int32" />
        <asp:Parameter Name="clientRecieved" Type="String" />
        <asp:Parameter Name="loadTotalKM" Type="Int32" />
        <asp:Parameter Name="note" Type="String" />
        <asp:Parameter Name="loadID" Type="Int32" />
    </UpdateParameters>

Thanks in advance!

10
  • I don't think you need the sql-server tag Commented Apr 18, 2016 at 6:33
  • Why not? It is all database related. The data in the grid view comes from a connected sql database. Commented Apr 18, 2016 at 6:39
  • put some code. so that we can get a better idea Commented Apr 18, 2016 at 6:53
  • On Edit, column you can calculate the value you want to modify then call update statement for the same table and bind the GridView again. Commented Apr 18, 2016 at 7:15
  • @Rojalin Sahoo is there anyway you could show me how that would be done? Commented Apr 18, 2016 at 7:17

2 Answers 2

1

Method 1: Creating Stored Procedure

Create Proc _SPUpdateTableName
@id int,
@truckvalue numeric(18,0)
as 
 begin
   Update TableName set  KMPerLiter = TotaKM/(@truckvalue+TrailerDiesel) , TruckDiesel = @truckvalue  where ID =@id
end

On SqlDataSource code :

<asp:SqlDataSource ID="LocalServerDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>" UpdateCommand="_SPUpdateTableName" UpdateCommandType="StoredProcedure">
        <UpdateParameters>
            <asp:Parameter Name="id" Type="Int32" />
            <asp:Parameter Name="truckvalue " Type="Decimal" />
        </UpdateParameters>
    </asp:SqlDataSource>

Or Else in RowUpdate Method you can calculate the parameters and assign them to your UpdateCommand statement Parameter :

Method 2:

  protected void grdvechile_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                decimal truckvalue= Convert.ToDecimal( grdvechile.Rows[e.RowIndex].Cells[1].Text);
               // fetch all value as needed and typecast
               decimal KMPerLiter = TotaKM/(truckvalue+TrailerDiesel);
               //Pass your required parameter
                LocalServerDataSource.UpdateParameters["truckvalue"].DefaultValue = truckvalue;
                LocalServerDataSource.UpdateParameters["KMPerLiter "].DefaultValue = KMPerLiter ;
                LocalServerDataSource.Update();
            }
            catch { }
        }

Note: These code is only on logic I thought will be appicable for your situation not tested.Change as per your Requirment.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Rojalin! Great answer.
0

No need.. Only create a virtual table to pick up the Grid view value, Then change it according to your requirement, again reassign it to Grid view

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.