Hi akshay806,
As per your query i have create the sample. If you have any doubt Please revert me back. Below is the given sample.
HTML
<asp:GridView ID="gvCalculation" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID" />
<asp:BoundField DataField="Items" HeaderText="Items" />
<asp:BoundField DataField="Price" HeaderText="Price of one Item" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
<asp:BoundField DataField="Total" HeaderText="TotalPrice" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnCalculate" Text="Sum" runat="server" OnClick="Sum" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Items", typeof(string));
dt.Columns.Add("Price", typeof(double));
dt.Columns.Add("Quantity", typeof(int));
dt.Columns.Add("Total", typeof(double));
dt.Rows.Add(1, "Plastic Pen", 10, 2);
dt.Rows.Add(2, "Shirts", 100, 2);
dt.Rows.Add(3, "Sandals", 200, 1);
dt.Rows.Add(4, "Shoes", 500, 2);
gvCalculation.DataSource = dt;
gvCalculation.DataBind();
}
protected void Sum(object sender, EventArgs e)
{
Button btn = (sender as Button);
double price = Convert.ToDouble((btn.NamingContainer as GridViewRow).Cells[2].Text);
int qauntity = Convert.ToInt32((btn.NamingContainer as GridViewRow).Cells[3].Text);
double total = price * qauntity;
(btn.NamingContainer as GridViewRow).Cells[4].Text = total.ToString();
btn.Enabled = false;
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim dt As New DataTable()
dt.Columns.Add("Id", GetType(Integer))
dt.Columns.Add("Items", GetType(String))
dt.Columns.Add("Price", GetType(Double))
dt.Columns.Add("Quantity", GetType(Integer))
dt.Columns.Add("Total", GetType(Double))
dt.Rows.Add(1, "Plastic Pen", 10, 2)
dt.Rows.Add(2, "Shirts", 100, 2)
dt.Rows.Add(3, "Sandals", 200, 1)
dt.Rows.Add(4, "Shoes", 500, 2)
gvCalculation.DataSource = dt
gvCalculation.DataBind()
End Sub
Protected Sub Sum(sender As Object, e As EventArgs)
Dim btn As Button = TryCast(sender, Button)
Dim price As Double = Convert.ToDouble(TryCast(btn.NamingContainer, GridViewRow).Cells(2).Text)
Dim qauntity As Integer = Convert.ToInt32(TryCast(btn.NamingContainer, GridViewRow).Cells(3).Text)
Dim total As Double = price * qauntity
TryCast(btn.NamingContainer, GridViewRow).Cells(4).Text = total.ToString()
btn.Enabled = False
End Sub
Screenshot
