Answered by:
calculations when updating the gridview

Question
-
User1649851808 posted
I have a gridview that displays its fields from a table and allows for edit /update.. I want to make some calculation everytime any row is updated ..these calcualtion depends on columns in a gridview..so when pressing the edit the row turns in to text boxes and during edit i want following changes in edit text boxes(some calculations )then on pressing the update button in grid will update the database values, how can I do this??
<asp:GridView ID="GridView1" runat= "server" CellPadding="4" ForeColor="#333333" GridLines="None" Height="195px" AutoGenerateColumns="False" CellSpacing="1" DataKeyNames="TableID" onrowcancelingedit="GridView1_RowCancelingEdit" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"> <RowStyle BackColor="White" ForeColor="#003399" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="ChDUser" runat="server" /> </ItemTemplate> <HeaderTemplate> <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" oncheckedchanged="CheckBox1_CheckedChanged" /> </HeaderTemplate> </asp:TemplateField> <asp:CommandField ButtonType="Button" ShowEditButton="True" /> <asp:TemplateField HeaderText="رقم الموظف"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="اسم الموظف"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="الادارة"> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("Dept") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="الشهر"> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("AttendMonth") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="السنة"> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Bind("AttendYear") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Month_H" HeaderText="عدد الساعات في الشهر" /> <asp:TemplateField HeaderText="عدد ساعات الغياب"> <ItemTemplate> <asp:Label ID="Label6" runat="server" Text='<%# Bind("Absent_H") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="عدد ساعات نسيان البصمة"> <ItemTemplate> <asp:Label ID="Label7" runat="server" Text='<%# Bind("Miss_punch_H") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="عدد ساعات التأخير"> <ItemTemplate> <asp:Label ID="Label8" runat="server" Text='<%# Bind("Late_H") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Eid_H" HeaderText="الساعات المسترجعة والخاصة بإجازات العيد " /> <asp:BoundField DataField="train_H" HeaderText="ساعات التدريب والمأموريات" /> <asp:BoundField DataField="Manager_H" HeaderText="الساعات المسترجعة حسب توجيهات مدراء الادارات" /> <asp:TemplateField HeaderText="عدد الساعات المستحقة"> <ItemTemplate> <asp:Label ID="Label9" runat="server" Text='<%# Bind("Des_H") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="نسبة العمل"> <ItemTemplate> <asp:Label ID="Label10" runat="server" Text='<%# Bind("Percent") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Notes" HeaderText="الملاحظات" /> </Columns> <FooterStyle BackColor="#99CCCC" ForeColor="#003399" /> <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" /> <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" /> <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" /> <EditRowStyle BackColor="#2461BF" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView>
here is c# code for row updating :
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string TableID=GridView1.DataKeys[e.RowIndex].Value.ToString(); string Month_H = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text; string Eid_H = ((TextBox)GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text; string train_H = ((TextBox)GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text; string Manager_H = ((TextBox)GridView1.Rows[e.RowIndex].Cells[13].Controls[0]).Text; // string Des_H = ((TextBox)GridView1.Rows[e.RowIndex].Cells[14].Controls[0]).Text; // string Percent = ((TextBox)GridView1.Rows[e.RowIndex].Cells[15].Controls[0]).Text; string Notes = ((TextBox)GridView1.Rows[e.RowIndex].Cells[16].Controls[0]).Text; if (DA.UpdateAtten(TableID, Month_H, Eid_H, train_H, Manager_H,Notes) > 0) msg.Text = "updated successfully"; GridView1.EditIndex = -1; Databind();
Thursday, September 8, 2016 5:56 PM
Answers
-
User-1034726716 posted
during edit i want following changes in edit text boxesSetup some TextBox within EditItemTemplate so you could use the TextChanged event to do the calculation. If you're good at the client-side scripting, then you can also implement the calculation using JavaScript/jQuery.
This article may not do exactly as what you require, but it should give you some idea to get started: http://www.codeproject.com/Articles/1112018/How-To-Do-Calculations-in-GridView
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 9, 2016 3:07 PM -
User36583972 posted
Hi Issra Ahmed,
According to your description, As far as I know, use jquery can achieve this effect , it is more simple
and convenient than in behind code . you can use the keydown event to execute the calculation method,
and then displays the results in appropriate columns. Finally, click update, the data will save
into the database .HTML:
<head runat="server"> <title></title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> //execute keyup event , then calculate result and show in the column $(function () { $("#GridView1_TextBox2_0").keyup(function () { var num1 = $("#GridView1_TextBox1_0").val(); var num2 = $("#GridView1_TextBox2_0").val(); var num3 = num1 * num2; $("#GridView1_Label1_0").html(num3); }) }); $(function () { $("#GridView1_TextBox2_1").keyup(function () { var num1 = $("#GridView1_TextBox1_1").val(); var num2 = $("#GridView1_TextBox2_1").val(); var num3 = num1 * num2; $("#GridView1_Label1_1").html(num3); }) }); $(function () { $("#GridView1_TextBox2_2").keyup(function () { var num1 = $("#GridView1_TextBox1_2").val(); var num2 = $("#GridView1_TextBox2_2").val(); var num3 = num1 * num2; $("#GridView1_Label1_2").html(num3); }) }); $(function () { $("#GridView1_TextBox2_3").keyup(function () { var num1 = $("#GridView1_TextBox1_3").val(); var num2 = $("#GridView1_TextBox2_3").val(); var num3 = num1 * num2; $("#GridView1_Label1_3").html(num3); }) }); </script> <style type="text/css"> body { font-family: Arial; font-size: 10pt; } table { border: 1px solid #ccc; } table th { background-color: #F7F7F7; color: #333; font-weight: bold; } table th, table td { padding: 5px; border-color: #ccc; } </style> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="OnRowEditing"> <Columns> <asp:TemplateField HeaderText="Price"> <ItemTemplate> <asp:Label ID="Label2" Text='<%# Convert.ToInt32(Eval("Price"))%>' runat="server" Width="100px"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TextBox1" Text='<%# Convert.ToInt32(Eval("Price"))%>' runat="server" Width="100px"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Count"> <ItemTemplate> <asp:Label ID="Label3" Text='<%# Convert.ToInt32(Eval("Count"))%>' runat="server" Width="100px"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TextBox2" Text='<%# Convert.ToInt32(Eval("Count"))%>' runat="server" Width="100px"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Paymoney"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Convert.ToInt32(Eval("Price"))* Convert.ToDouble(Eval("Count")) %>' Width="100px"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Convert.ToInt32(Eval("Price"))* Convert.ToDouble(Eval("Count")) %>' Width="100px"></asp:Label> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:LinkButton Text="Edit" runat="server" CommandName="Edit" /> </ItemTemplate> <EditItemTemplate> <asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" /> <asp:LinkButton Text="Cancel" runat="server" OnClick="OnCancel" /> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </form> </body>
Code Behind:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); DataColumn dc1 = new DataColumn("Price", typeof(Int32)); DataColumn dc2 = new DataColumn("Count", typeof(Int32)); DataColumn dc3 = new DataColumn("Paymoney", typeof(Int32)); dt.Columns.Add(dc1); dt.Columns.Add(dc2); dt.Columns.Add(dc3); dt.Rows.Add(100, 3 ); dt.Rows.Add(222, 3); dt.Rows.Add(21, 4); dt.Rows.Add(3443,2); ViewState["dt"] = dt; this.BindGrid(); } } protected void BindGrid() { GridView1.DataSource = ViewState["dt"] as DataTable; GridView1.DataBind(); } protected void OnRowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; this.BindGrid(); } int Price; int Count; protected void OnUpdate(object sender, EventArgs e) { GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow; Price = Convert.ToInt32((row.Cells[0].Controls[1] as TextBox).Text); Count = Convert.ToInt32((row.Cells[1].Controls[1] as TextBox).Text); DataTable dt = ViewState["dt"] as DataTable; dt.Rows[row.RowIndex]["Price"] = Price; dt.Rows[row.RowIndex]["Count"] = Count; ViewState["dt"] = dt; GridView1.EditIndex = -1; this.BindGrid(); } protected void OnCancel(object sender, EventArgs e) { GridView1.EditIndex = -1; this.BindGrid(); } }
Screenshot :
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 11, 2016 12:52 PM
All replies
-
User-1034726716 posted
during edit i want following changes in edit text boxesSetup some TextBox within EditItemTemplate so you could use the TextChanged event to do the calculation. If you're good at the client-side scripting, then you can also implement the calculation using JavaScript/jQuery.
This article may not do exactly as what you require, but it should give you some idea to get started: http://www.codeproject.com/Articles/1112018/How-To-Do-Calculations-in-GridView
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 9, 2016 3:07 PM -
User36583972 posted
Hi Issra Ahmed,
According to your description, As far as I know, use jquery can achieve this effect , it is more simple
and convenient than in behind code . you can use the keydown event to execute the calculation method,
and then displays the results in appropriate columns. Finally, click update, the data will save
into the database .HTML:
<head runat="server"> <title></title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> //execute keyup event , then calculate result and show in the column $(function () { $("#GridView1_TextBox2_0").keyup(function () { var num1 = $("#GridView1_TextBox1_0").val(); var num2 = $("#GridView1_TextBox2_0").val(); var num3 = num1 * num2; $("#GridView1_Label1_0").html(num3); }) }); $(function () { $("#GridView1_TextBox2_1").keyup(function () { var num1 = $("#GridView1_TextBox1_1").val(); var num2 = $("#GridView1_TextBox2_1").val(); var num3 = num1 * num2; $("#GridView1_Label1_1").html(num3); }) }); $(function () { $("#GridView1_TextBox2_2").keyup(function () { var num1 = $("#GridView1_TextBox1_2").val(); var num2 = $("#GridView1_TextBox2_2").val(); var num3 = num1 * num2; $("#GridView1_Label1_2").html(num3); }) }); $(function () { $("#GridView1_TextBox2_3").keyup(function () { var num1 = $("#GridView1_TextBox1_3").val(); var num2 = $("#GridView1_TextBox2_3").val(); var num3 = num1 * num2; $("#GridView1_Label1_3").html(num3); }) }); </script> <style type="text/css"> body { font-family: Arial; font-size: 10pt; } table { border: 1px solid #ccc; } table th { background-color: #F7F7F7; color: #333; font-weight: bold; } table th, table td { padding: 5px; border-color: #ccc; } </style> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="OnRowEditing"> <Columns> <asp:TemplateField HeaderText="Price"> <ItemTemplate> <asp:Label ID="Label2" Text='<%# Convert.ToInt32(Eval("Price"))%>' runat="server" Width="100px"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TextBox1" Text='<%# Convert.ToInt32(Eval("Price"))%>' runat="server" Width="100px"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Count"> <ItemTemplate> <asp:Label ID="Label3" Text='<%# Convert.ToInt32(Eval("Count"))%>' runat="server" Width="100px"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TextBox2" Text='<%# Convert.ToInt32(Eval("Count"))%>' runat="server" Width="100px"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Paymoney"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Convert.ToInt32(Eval("Price"))* Convert.ToDouble(Eval("Count")) %>' Width="100px"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Convert.ToInt32(Eval("Price"))* Convert.ToDouble(Eval("Count")) %>' Width="100px"></asp:Label> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:LinkButton Text="Edit" runat="server" CommandName="Edit" /> </ItemTemplate> <EditItemTemplate> <asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" /> <asp:LinkButton Text="Cancel" runat="server" OnClick="OnCancel" /> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </form> </body>
Code Behind:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); DataColumn dc1 = new DataColumn("Price", typeof(Int32)); DataColumn dc2 = new DataColumn("Count", typeof(Int32)); DataColumn dc3 = new DataColumn("Paymoney", typeof(Int32)); dt.Columns.Add(dc1); dt.Columns.Add(dc2); dt.Columns.Add(dc3); dt.Rows.Add(100, 3 ); dt.Rows.Add(222, 3); dt.Rows.Add(21, 4); dt.Rows.Add(3443,2); ViewState["dt"] = dt; this.BindGrid(); } } protected void BindGrid() { GridView1.DataSource = ViewState["dt"] as DataTable; GridView1.DataBind(); } protected void OnRowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; this.BindGrid(); } int Price; int Count; protected void OnUpdate(object sender, EventArgs e) { GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow; Price = Convert.ToInt32((row.Cells[0].Controls[1] as TextBox).Text); Count = Convert.ToInt32((row.Cells[1].Controls[1] as TextBox).Text); DataTable dt = ViewState["dt"] as DataTable; dt.Rows[row.RowIndex]["Price"] = Price; dt.Rows[row.RowIndex]["Count"] = Count; ViewState["dt"] = dt; GridView1.EditIndex = -1; this.BindGrid(); } protected void OnCancel(object sender, EventArgs e) { GridView1.EditIndex = -1; this.BindGrid(); } }
Screenshot :
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 11, 2016 12:52 PM