PDA

View Full Version : جمع مقادیر فیلد در DataGrid



shabnam_f
دوشنبه 15 فروردین 1384, 16:09 عصر
آیا در datagridمیشه یک سطر برای جمع بستن مقادیر هرستون ایجاد کرد ؟بدون نیاز به دستکاری query.

Behrouz_Rad
دوشنبه 15 فروردین 1384, 17:24 عصر
مثال:


PROTECTED As sender myDataGrid_ItemDataBound(ByVal Sub System.Object,
ByVal e As DataGridItemEventArgs) Handles myDataGrid.ItemDataBound
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem
interimTotal += CType(e.Item.Cells(3).Text, Double)
Case ListItemType.Footer
e.Item.Cells(2).Text = "TOTAL: "
e.Item.Cells(3).Text = interimTotal.ToString
End Select
End Sub

fereshte22
سه شنبه 18 اردیبهشت 1386, 15:30 عصر
سلام
من میخواستم جمع مقادیر یک ستون را در gridview به دست اورم.سوالم اینه که ایا از کدی که آقای راد در بالا برای datagrid داده اند میتونم استفاده کنم؟
چه تغییری در ان باید اعمال کنم؟
در ضمن من کد بالا رابرای datagrid امتحان کردم ولی جمع ستون را محاسبه نکرد.
کسی میتونه من را راهنمایی کند؟

fereshte22
چهارشنبه 19 اردیبهشت 1386, 09:47 صبح
مشکلم حل شد .با کد زیر


<asp:GridView ID="GridView1"
ShowFooter="true" DataKeyNames="ProductId"
AutoGenerateColumns="false" runat="server"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Productid" HeaderText="Product Id" />
<asp:BoundField DataField="ProductName" FooterText="Total" HeaderText="Product Name" />
<asp:TemplateField HeaderText="Unit Price" FooterStyle-Font-Bold="True">
<ItemTemplate>
<%# GetUnitPrice(decimal.Parse(Eval("UnitPrice").ToString())).ToString("N2") %>
</ItemTemplate>
<FooterTemplate>
<%# GetTotal().ToString("N2") %>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DummyDB %>"
SelectCommand="Select * from Products">
</asp:SqlDataSource>

کد vb.net


Dim TotalUnitPrice As Decimal = 0.0
Function GetUnitPrice(ByVal Price As Decimal) As Decimal
TotalUnitPrice += Price
Return Price
End Function
Function GetTotal() As Decimal
Return TotalUnitPrice
End Function

کد سی شارپ


decimal TotalUnitPrice;
decimal GetUnitPrice(decimal Price)
{
TotalUnitPrice += Price;
return Price;
}
decimal GetTotal()
{
return TotalUnitPrice;
}