a 1 1 1
b 1 1 1
c 1 1 1
合计 3 3 3
请问大家,这如何实现?谢谢!
6 个解决方案
#1
没有用过!帮顶学习一下!
#2
高手们帮指导一下啊,或看看有别的方法实现这个目标!
#3
看看吧:
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
Dim Cnn as OleDbConnection
Private Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\nhonline\datagrid\web_article\testcodes\14_addingcontrolstodatagridfooteritem\data.mdb"
Dim cnn As New OleDbConnection(connstr)
Dim da As New OleDbDataAdapter("select top 5 * from products_2", cnn)
Dim ds As New DataSet
da.Fill(ds, "Products")
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub
Function GetTotalPrice()
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\nhonline\datagrid\web_article\testcodes\14_addingcontrolstodatagridfooteritem\data.mdb"
Dim cnn As New OleDbConnection(connstr)
'Dim TotalPrice as Integer
Dim TotalPrice as double
Dim CmdDataCount as OleDbCommand
Cnn.Open()
CmdDataCount = New OleDbCommand("SELECT SUM(unitprice) FROM products_2",Cnn)
TotalPrice = CmdDataCount.ExecuteScalar()
Cnn.Close
Return (TotalPrice)
End Function
Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
If (e.Item.ItemType = ListItemType.Footer) Then
e.Item.Cells(2).Text = "Total "
e.Item.Cells(2).HorizontalAlign = HorizontalAlign.Right
Dim oTextbox As New TextBox
oTextbox.Width = New Unit(100, UnitType.Pixel)
'oTextbox.Text = "90.3500dd" 'This can be changed to populate from some datasource.
oTextbox.Text = GetTotalPrice()
e.Item.Cells(3).Controls.Add(oTextbox)
End If
End Sub
</script>
<Form runat="server">
<asp:datagrid id="DataGrid1"
runat="server"
width="500px"
AllowPaging="False"
ItemStyle-CssClass="tableItem"
HeaderStyle-CssClass="tableHeader"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="False"
DataKeyField="ProductID"
ShowFooter="True"
BorderWidth="0"
OnItemCreated="DataGrid1_ItemCreated"
>
<Columns>
<asp:TemplateColumn headertext="Product ID">
<ItemTemplate>
<asp:TextBox style="width:100px;" id="ProductID" runat="server"
Text='<%# Container.DataItem("ProductID") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Product Name">
<ItemTemplate>
<asp:TextBox style="width:200px;" id="ProductName" runat="server"
Text='<%# Container.DataItem("ProductName") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Quantity" FooterStyle-HorizontalAlign=Right >
<ItemTemplate>
<asp:TextBox style="width:100px;" id="Quantity" runat="server"
Text='<%# Container.DataItem("QuantityPerUnit") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Product Price" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox style="width:100px;" id="ProductPrice" runat="server"
Text='<%# Container.DataItem("UnitPrice") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
</Form>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
Dim Cnn as OleDbConnection
Private Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\nhonline\datagrid\web_article\testcodes\14_addingcontrolstodatagridfooteritem\data.mdb"
Dim cnn As New OleDbConnection(connstr)
Dim da As New OleDbDataAdapter("select top 5 * from products_2", cnn)
Dim ds As New DataSet
da.Fill(ds, "Products")
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub
Function GetTotalPrice()
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\nhonline\datagrid\web_article\testcodes\14_addingcontrolstodatagridfooteritem\data.mdb"
Dim cnn As New OleDbConnection(connstr)
'Dim TotalPrice as Integer
Dim TotalPrice as double
Dim CmdDataCount as OleDbCommand
Cnn.Open()
CmdDataCount = New OleDbCommand("SELECT SUM(unitprice) FROM products_2",Cnn)
TotalPrice = CmdDataCount.ExecuteScalar()
Cnn.Close
Return (TotalPrice)
End Function
Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
If (e.Item.ItemType = ListItemType.Footer) Then
e.Item.Cells(2).Text = "Total "
e.Item.Cells(2).HorizontalAlign = HorizontalAlign.Right
Dim oTextbox As New TextBox
oTextbox.Width = New Unit(100, UnitType.Pixel)
'oTextbox.Text = "90.3500dd" 'This can be changed to populate from some datasource.
oTextbox.Text = GetTotalPrice()
e.Item.Cells(3).Controls.Add(oTextbox)
End If
End Sub
</script>
<Form runat="server">
<asp:datagrid id="DataGrid1"
runat="server"
width="500px"
AllowPaging="False"
ItemStyle-CssClass="tableItem"
HeaderStyle-CssClass="tableHeader"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="False"
DataKeyField="ProductID"
ShowFooter="True"
BorderWidth="0"
OnItemCreated="DataGrid1_ItemCreated"
>
<Columns>
<asp:TemplateColumn headertext="Product ID">
<ItemTemplate>
<asp:TextBox style="width:100px;" id="ProductID" runat="server"
Text='<%# Container.DataItem("ProductID") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Product Name">
<ItemTemplate>
<asp:TextBox style="width:200px;" id="ProductName" runat="server"
Text='<%# Container.DataItem("ProductName") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Quantity" FooterStyle-HorizontalAlign=Right >
<ItemTemplate>
<asp:TextBox style="width:100px;" id="Quantity" runat="server"
Text='<%# Container.DataItem("QuantityPerUnit") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Product Price" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox style="width:100px;" id="ProductPrice" runat="server"
Text='<%# Container.DataItem("UnitPrice") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
</Form>
#4
在DataGrid的数据源上加合计行,具体的方法你可以在SQL语句添加,也可以在DataTable中手动添加,手动添加时请参考:
http://www.csdn.net/Develop/read_article.asp?id=22710
http://www.csdn.net/Develop/read_article.asp?id=22710
#5
用SQL语句加合计行,查询后再赋给DataGrid.试一下吧
#6
也可以把 页脚 做成合计行,页脚第一列的text值为合计,其它列为datagrid中对应列各行的和。
#1
没有用过!帮顶学习一下!
#2
高手们帮指导一下啊,或看看有别的方法实现这个目标!
#3
看看吧:
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
Dim Cnn as OleDbConnection
Private Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\nhonline\datagrid\web_article\testcodes\14_addingcontrolstodatagridfooteritem\data.mdb"
Dim cnn As New OleDbConnection(connstr)
Dim da As New OleDbDataAdapter("select top 5 * from products_2", cnn)
Dim ds As New DataSet
da.Fill(ds, "Products")
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub
Function GetTotalPrice()
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\nhonline\datagrid\web_article\testcodes\14_addingcontrolstodatagridfooteritem\data.mdb"
Dim cnn As New OleDbConnection(connstr)
'Dim TotalPrice as Integer
Dim TotalPrice as double
Dim CmdDataCount as OleDbCommand
Cnn.Open()
CmdDataCount = New OleDbCommand("SELECT SUM(unitprice) FROM products_2",Cnn)
TotalPrice = CmdDataCount.ExecuteScalar()
Cnn.Close
Return (TotalPrice)
End Function
Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
If (e.Item.ItemType = ListItemType.Footer) Then
e.Item.Cells(2).Text = "Total "
e.Item.Cells(2).HorizontalAlign = HorizontalAlign.Right
Dim oTextbox As New TextBox
oTextbox.Width = New Unit(100, UnitType.Pixel)
'oTextbox.Text = "90.3500dd" 'This can be changed to populate from some datasource.
oTextbox.Text = GetTotalPrice()
e.Item.Cells(3).Controls.Add(oTextbox)
End If
End Sub
</script>
<Form runat="server">
<asp:datagrid id="DataGrid1"
runat="server"
width="500px"
AllowPaging="False"
ItemStyle-CssClass="tableItem"
HeaderStyle-CssClass="tableHeader"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="False"
DataKeyField="ProductID"
ShowFooter="True"
BorderWidth="0"
OnItemCreated="DataGrid1_ItemCreated"
>
<Columns>
<asp:TemplateColumn headertext="Product ID">
<ItemTemplate>
<asp:TextBox style="width:100px;" id="ProductID" runat="server"
Text='<%# Container.DataItem("ProductID") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Product Name">
<ItemTemplate>
<asp:TextBox style="width:200px;" id="ProductName" runat="server"
Text='<%# Container.DataItem("ProductName") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Quantity" FooterStyle-HorizontalAlign=Right >
<ItemTemplate>
<asp:TextBox style="width:100px;" id="Quantity" runat="server"
Text='<%# Container.DataItem("QuantityPerUnit") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Product Price" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox style="width:100px;" id="ProductPrice" runat="server"
Text='<%# Container.DataItem("UnitPrice") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
</Form>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
Dim Cnn as OleDbConnection
Private Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\nhonline\datagrid\web_article\testcodes\14_addingcontrolstodatagridfooteritem\data.mdb"
Dim cnn As New OleDbConnection(connstr)
Dim da As New OleDbDataAdapter("select top 5 * from products_2", cnn)
Dim ds As New DataSet
da.Fill(ds, "Products")
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub
Function GetTotalPrice()
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\nhonline\datagrid\web_article\testcodes\14_addingcontrolstodatagridfooteritem\data.mdb"
Dim cnn As New OleDbConnection(connstr)
'Dim TotalPrice as Integer
Dim TotalPrice as double
Dim CmdDataCount as OleDbCommand
Cnn.Open()
CmdDataCount = New OleDbCommand("SELECT SUM(unitprice) FROM products_2",Cnn)
TotalPrice = CmdDataCount.ExecuteScalar()
Cnn.Close
Return (TotalPrice)
End Function
Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
If (e.Item.ItemType = ListItemType.Footer) Then
e.Item.Cells(2).Text = "Total "
e.Item.Cells(2).HorizontalAlign = HorizontalAlign.Right
Dim oTextbox As New TextBox
oTextbox.Width = New Unit(100, UnitType.Pixel)
'oTextbox.Text = "90.3500dd" 'This can be changed to populate from some datasource.
oTextbox.Text = GetTotalPrice()
e.Item.Cells(3).Controls.Add(oTextbox)
End If
End Sub
</script>
<Form runat="server">
<asp:datagrid id="DataGrid1"
runat="server"
width="500px"
AllowPaging="False"
ItemStyle-CssClass="tableItem"
HeaderStyle-CssClass="tableHeader"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="False"
DataKeyField="ProductID"
ShowFooter="True"
BorderWidth="0"
OnItemCreated="DataGrid1_ItemCreated"
>
<Columns>
<asp:TemplateColumn headertext="Product ID">
<ItemTemplate>
<asp:TextBox style="width:100px;" id="ProductID" runat="server"
Text='<%# Container.DataItem("ProductID") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Product Name">
<ItemTemplate>
<asp:TextBox style="width:200px;" id="ProductName" runat="server"
Text='<%# Container.DataItem("ProductName") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Quantity" FooterStyle-HorizontalAlign=Right >
<ItemTemplate>
<asp:TextBox style="width:100px;" id="Quantity" runat="server"
Text='<%# Container.DataItem("QuantityPerUnit") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Product Price" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox style="width:100px;" id="ProductPrice" runat="server"
Text='<%# Container.DataItem("UnitPrice") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
</Form>
#4
在DataGrid的数据源上加合计行,具体的方法你可以在SQL语句添加,也可以在DataTable中手动添加,手动添加时请参考:
http://www.csdn.net/Develop/read_article.asp?id=22710
http://www.csdn.net/Develop/read_article.asp?id=22710
#5
用SQL语句加合计行,查询后再赋给DataGrid.试一下吧
#6
也可以把 页脚 做成合计行,页脚第一列的text值为合计,其它列为datagrid中对应列各行的和。