Merhaba
arkadaşlar bu makalemizde GridView nesnesinde Quantity sütunundaki değerleri toplamını
alt kısma yazdıracağız. Gridview nesnesinin AutoGenerateColumns özelliğini
false yapın. GridView nesnesinin Columns kısmına,
<FooterTemplate>
<div style="text-align: right;">
<asp:Label ID="lblTotal" runat="server" />
</div>
</FooterTemplate>
ekleyeceğiz.
Buradaki label e sütunun toplamı yazılacak. Gridview_RowDataBound kısmına
kodumuzu yazıyoruz.
Screenshot
Şekil 1
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
int total = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getBind();
}
}
protected void getBind()
{
SqlConnection baglan = new SqlConnection("Data
Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated
Security=True;Connect Timeout=30");
SqlCommand komut = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter(komut);
DataSet ds = new DataSet();
komut.CommandText = "Select
Distinct Top 8 OrderNo,ProductId,Product,Quantity from [dbo].[Products]
Group By OrderNo,ProductId,Product,Quantity";
komut.Connection = baglan;
baglan.Open();
komut.ExecuteNonQuery();
da.Fill(ds);
baglan.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblqy = (Label)e.Row.FindControl("lblQuantity");
int qty = Int32.Parse(lblqy.Text);
total = total + qty;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotal = (Label)e.Row.FindControl("lblTotal");
lblTotal.Text ="Toplam ( Total ) : " + total.ToString();
}
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound" CellPadding="4" ForeColor="#333333" GridLines="None" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="OrderNo" HeaderText="OrderNo" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="ProductId" HeaderText="ProductId" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Product" HeaderText="Product" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<div style="text-align: right;">
<asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity") %>' />
</div>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: right;">
<asp:Label ID="lblTotal" runat="server" />
</div>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede
görüşmek üzere. Bahadır ŞAHİN
0 comments:
Yorum Gönder