<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%">
<Columns>
<asp:TemplateField HeaderText="单价">
<ItemTemplate>
<asp:Label ID="lblHT_DJ" runat="server" Style="text-align: right" Enabled="false"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" VerticalAlign="Bottom" />
</asp:TemplateField>
<asp:TemplateField HeaderText="数量">
<ItemTemplate>
<asp:TextBox ID="txtHT_SL" Width="70px" runat="server" Style="text-align: right"
onkeypress="if ((event.keyCode < 48 || event.keyCode >57)&& event.keyCode!=43 && event.keyCode!=45 && event.keyCode!=46) event.returnValue = false;"
onpaste="return false" onfocus="javascript:this.select()"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="70px" Wrap="False" />
<FooterStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="合计)">
<ItemTemplate>
<asp:TextBox ID="txtFK_BQ_JE" Width="70px" runat="server" Style="text-align: right"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="70px" Wrap="False" />
<FooterStyle HorizontalAlign="Right" />
</asp:TemplateField>在数量列输入数据之后与单价列的数据相乘 自动汇总到合计列.
sql的办法
如果一定要在vs上实现,那么就在RowDataBound事件里写
((TextBox)e.row.FindControl("txtFK_BQ_JE")).Text =
(Convert.ToDouble(((Lable)e.row.FindControl("lblHT_DJ")).Text.ToString()) *
Convert.ToDouble(((TextBox)e.row.FindControl("txtHT_SL")).Text.ToString())).tostring()
{
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lb = (Label)e.Row.FindControl("Label1");
int sun=0;
foreach (DataRow dr in dt.Rows)
{
sun = sun + System.Convert.ToInt32(dr[0]);
}
lb.Text = "总计:"+sun.ToString();
}
}自动计算
function Cal(tb) {
var b = parseFloat(tb.value);
if (!isNaN(b)) {
var p1 = tb.parentElement || tb.parentNode;
var par = p1.parentElement || p1.parentNode;
var a = parseInt(par.childNodes[0].getElementsByTagName("span")[0].innerHTML);
var tbC = par.childNodes[2].getElementsByTagName("input")[0]; tbC.value = a * b;
}
}
textbox onkeyup="Cal(this)"
{ System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("id", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("Score", typeof(System.Double)));
dt.Columns.Add(new System.Data.DataColumn("单价", typeof(System.Int32)));
System.Random rd = new System.Random();
for (int i = 1; i < 6; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "【孟子E章】" + i.ToString();
dr[2] = System.Math.Ceiling(rd.NextDouble() * 100);
dr[3] = i * i;
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
GridView1.DataSource = dv;
GridView1.DataBind(); }
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox t = e.Row.FindControl("txtHT_SL") as TextBox;
t.Attributes.Add("onblur", "Sum(" +( e.Row.DataItemIndex+1 )+ ",this)");
}
}
</script><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <script type="text/javascript">
function Sum(m, o) {
d = document.getElementById("<%=GridView1.ClientID %>");
dj = isNaN(parseInt(d.rows[m].cells[0].getElementsByTagName("SPAN")[0].innerHTML, 10)) ? 0 : parseInt(d.rows[m].cells[0].getElementsByTagName("SPAN")[0].innerHTML, 10);
sl = isNaN(parseInt(o.value, 10)) ? 0 : parseInt(o.value, 10);
d.rows[m].cells[2].getElementsByTagName("INPUT")[0].value = dj * sl;
o.value = sl;
}
</script></head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="单价">
<ItemTemplate>
<asp:Label ID="lblHT_DJ" runat="server"><%#Eval("单价") %></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" VerticalAlign="Bottom" />
</asp:TemplateField>
<asp:TemplateField HeaderText="数量">
<ItemTemplate>
<asp:TextBox ID="txtHT_SL" Width="70px" runat="server"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="70px" Wrap="False" />
<FooterStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="合计)">
<ItemTemplate>
<asp:TextBox ID="txtFK_BQ_JE" Width="70px" runat="server" Style="text-align: right"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="70px" Wrap="False" />
<FooterStyle HorizontalAlign="Right" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Page_Load(object sender, EventArgs e)
{ System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("id", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("Score", typeof(System.Double)));
dt.Columns.Add(new System.Data.DataColumn("单价", typeof(System.Int32)));
System.Random rd = new System.Random();
for (int i = 1; i < 6; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "【孟子E章】" + i.ToString();
dr[2] = System.Math.Ceiling(rd.NextDouble() * 100);
dr[3] = i * i;
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
GridView1.DataSource = dv;
GridView1.DataBind(); }
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox t = e.Row.FindControl("txtHT_SL") as TextBox;
TextBox j = e.Row.FindControl("txtFK_BQ_JE") as TextBox;
t.Attributes.Add("onblur", "Sum(this," + DataBinder.Eval(e.Row.DataItem, "单价") + ",'" + j.ClientID + "')");
}
}
</script><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <script type="text/javascript">
function Sum(o, a, b) {
sl = isNaN(parseInt(o.value, 10)) ? 0 : parseInt(o.value, 10);
document.getElementById(b).value = sl * a;
o.value = sl;
}
</script></head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="单价">
<ItemTemplate>
<asp:Label ID="lblHT_DJ" runat="server"><%#Eval("单价") %></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" VerticalAlign="Bottom" />
</asp:TemplateField>
<asp:TemplateField HeaderText="数量">
<ItemTemplate>
<asp:TextBox ID="txtHT_SL" Width="70px" runat="server"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="70px" Wrap="False" />
<FooterStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="合计)">
<ItemTemplate>
<asp:TextBox ID="txtFK_BQ_JE" Width="70px" runat="server" Style="text-align: right"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="70px" Wrap="False" />
<FooterStyle HorizontalAlign="Right" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>