Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to muliply two text box and show the result in next column of grid 1

Status
Not open for further replies.

mazez1971

Programmer
Dec 21, 2009
1
BD
there are 5 columns in a grid view
1. product, 2.Unit 3.qty 4.price 5.total
as the user will enter the qty and price in 3rd and 4th column the system will calculate qty X price and show it the 5th column

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns="<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 600px; position: relative; top: 0px;">
<tr>
<td> </td>
<td > </td>
<td >
<asp:Button ID="cmdSubmit" runat="server" Font-Names="Verdana" Font-Size="10pt" Style="left: 96px;
position: relative; top: 24px" Text="Submit" OnClick="cmdSubmit_Click" />
<asp:Button ID="cmdNew" runat="server" Font-Names="Verdana" Font-Size="10pt" Style="left: 96px;
position: relative; top: 24px" Text="New" Width="67px" OnClick="cmdNew_Click" /></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td style="width: 117px">
<asp:Label ID="Label3" runat="server" Font-Names="Verdana" Font-Size="10pt" Style="position: relative; left: 0px;"
Text="Search" Width="136px"></asp:Label></td>
<td style="width: 248px">
<asp:TextBox ID="TextBox1" runat="server" Font-Names="Verdana" Font-Size="10pt" Style="position: relative"
Width="72px"></asp:TextBox></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td style="width: 117px">
<asp:Label ID="Label1" runat="server" Font-Names="Verdana" Font-Size="10pt" Style="position: relative; left: 0px;"
Text="Serial No" Width="136px"></asp:Label></td>
<td style="width: 248px">
<asp:TextBox ID="PIID" runat="server" Enabled="False" Font-Names="Verdana" Font-Size="10pt"
Style="position: relative"></asp:TextBox></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td style="width: 117px">
<asp:Label ID="Label2" runat="server" Font-Names="Verdana" Font-Size="10pt" Style="position: relative; left: 0px;"
Text="P I No" Width="136px"></asp:Label></td>
<td style="width: 248px">
<asp:TextBox ID="PINo" runat="server" Font-Names="Verdana" Font-Size="10pt" Style="position: relative">0</asp:TextBox></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td style="width: 117px">
<asp:Label ID="Label4" runat="server" Font-Names="Verdana" Font-Size="10pt" Style="left: 0px;
position: relative" Text="Customer" Width="136px"></asp:Label></td>
<td style="width: 248px">
<asp:DropDownList ID="piCustomerID" runat="server" Style="position: relative" Width="248px">
</asp:DropDownList></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<br />

<table style="width: 600px; position: relative">
<tr>
<td> </td>
<td style="width: 600px">
 

<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">

<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Serial" />
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:DropDownList ID="pipProduct" runat="server" AppendDataBoundItems="true">
<asp:ListItem Value="-1">Select</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Unit">
<ItemTemplate>
<asp:DropDownList ID="pipUnit" runat="server" AppendDataBoundItems="true">
<asp:ListItem Value="-1"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Quantity">

<ItemTemplate>
<asp:TextBox ID="pipQty" runat="server" Width="76px">0</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:TextBox ID="pipPrice" runat="server" Width="76px">0</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Total">
<ItemTemplate>

<asp:TextBox ID="pipTotal" runat="server" Width="76px">0</asp:TextBox>

<asp:Button ID="ButtonDel" runat="server" Text="Delete"
onclick="ButtonDel_Click"/>
</ItemTemplate>

<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add Item"
onclick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>



-------------------------
c# code
-------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
using System.Web.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected double GetResult(string _pipQty, string _pipPrice)
{
return Convert.ToDouble(_pipQty) * Convert.ToDouble(_pipPrice);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
PINo.Focus();
cmdSubmit.Attributes.Add("onClick", "return confirm('You sure to save the record?');");
}
}
private ArrayList GetDummyData()
{
ArrayList arr = new ArrayList();
arr.Add(new ListItem("Product1", "1"));
arr.Add(new ListItem("Product2", "2"));
return arr;
}

private void FillDropDownList(DropDownList ddl)
{
ArrayList arr = GetDummyData();
foreach (ListItem item in arr)
{
ddl.Items.Add(item);
}
}

private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(Int32)));
dt.Columns.Add(new DataColumn("Column4", typeof(Int32)));
dt.Columns.Add(new DataColumn("Column5", typeof(Int32)));
dr = dt.NewRow();
dr["RowNumber"] = 1;

dr["Column3"] = 0;
dr["Column4"] = 0;
dr["Column5"] = 0;

dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();

DropDownList ddl1 = (DropDownList)Gridview1.Rows[0].Cells[1].FindControl("pipProduct");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[0].Cells[2].FindControl("pipUnit");

FillDropDownList(ddl1);
FillDropDownList(ddl2);
}

private void AddNewRowToGrid()
{
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
{
DropDownList ddl1 = (DropDownList)Gridview1.Rows.Cells[1].FindControl("pipProduct");
DropDownList ddl2 = (DropDownList)Gridview1.Rows.Cells[2].FindControl("pipUnit");
TextBox pipQtyV = (TextBox)Gridview1.Rows.Cells[3].FindControl("pipQty");
TextBox pipPriceV = (TextBox)Gridview1.Rows.Cells[4].FindControl("pipPrice");
TextBox pipTotalV = (TextBox)Gridview1.Rows.Cells[5].FindControl("pipTotal");

dtCurrentTable.Rows["Column1"] = ddl1.SelectedItem.Text;
dtCurrentTable.Rows["Column2"] = ddl2.SelectedItem.Text;
dtCurrentTable.Rows["Column3"] = pipQtyV.Text;
dtCurrentTable.Rows["Column4"] = pipPriceV.Text;
dtCurrentTable.Rows["Column5"] = pipTotalV.Text;
}
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}

private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("pipProduct");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("pipUnit");

TextBox pipQtyV = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("pipQty");
TextBox pipPriceV = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("pipPrice");
TextBox pipTotalV = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("pipTotal");

pipQtyV.Text = dt.Rows["Column3"].ToString();
pipPriceV.Text = dt.Rows["Column4"].ToString();
pipTotalV.Text = dt.Rows["Column5"].ToString();

FillDropDownList(ddl1);
FillDropDownList(ddl2);
if (i < dt.Rows.Count - 1)
{
ddl1.ClearSelection();
ddl1.Items.FindByText(dt.Rows["Column1"].ToString()).Selected = true;

ddl2.ClearSelection();
ddl2.Items.FindByText(dt.Rows["Column2"].ToString()).Selected = true;
}
rowIndex++;
}
}
}
}

protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}

protected void ButtonDel_Click(object sender, EventArgs e)
{
Button lb = (Button)sender;
GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
int rowID = gvRow.RowIndex;

if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 1)
{
if (gvRow.RowIndex < dt.Rows.Count - 1)
{
//Remove the Selected Row data
dt.Rows.Remove(dt.Rows[rowID]);
ResetRowID(dt);
}
}
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
SetPreviousData();
}
private void ResetRowID(DataTable dt)
{
int rowNumber = 1;
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
row[0] = rowNumber;
rowNumber++;
}
}
}
protected void cmdNew_Click(object sender, EventArgs e)
{
}
protected void cmdSubmit_Click(object sender, EventArgs e)
{
}
}
 
there is alot of noise in this markup and code.
1. use css to format the elements rather than inline styling and tables.
2. i would also attempt to remove as much of this ([tt](DropDownList)Gridview1.Rows.Cells[1].FindControl("pipProduct")[/tt]) as possible. pull the data from the source, not the presentation.

as for the calculations. your javascript library of choice (jquery, etc) to get the values, do the math, and set the total. this will make for clean user experience so you don't need a request just to add two numbers.
I would also make this a calculated column on the underlying datatable and bind the gridview column to the datatable's calculated column
example
Code:
var table = new DataTable();
table.Columns.Add("x", typeof(int));
table.Columns.Add("y", typeof(int));

var column = tabls.NewColumn();
column.Name = "total";
column.ColumnType = typeof(int);
column.ReadOnly = true;
column.Expression = "[x]+[y]";
table.Columns.Add(column);

tabls.Rows.Add(new object[]{1, 2});
tabls.Rows.Add(new object[]{3, 4});
tabls.Rows.Add(new object[]{5, 6});

MyGrid.DataSource = table;
DataBind();
Code:
<asp:gridview ...>
   <columns>
      <asp:template>
           <asp:textbox text='<%#bind("x")%>' CssClass="add-together"/>
      </asp:template>
      <asp:template>
           <asp:textbox text='<%#bind("y")%>' CssClass="add-together"/>
      </asp:template>
      <asp:template>
           <asp:label text='<%#eval("total")%>' CssClass="total"/>
      </asp:template>
   </columns>
</asp:gridview>
Code:
var $a = $(':text.add-together').blur(function(){
    var $b = $a.closest(':text.add-together');
    var $total = $a.closest('.total');
    $total.text($a.val()+$b.val());
});
this is all from memory so i'm sure there are bugs, but this is the general idea.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top