İyi Günler bu makalemizde tablomuzdaki stokId numaraları aynı olan ürünlerin alt toplam tutarlarını hemen bir sonraki alt satırda ve toplam tutarıda tablomuzun en alt kısmında yani Gridview nesnesinin footer kısmında göstereceğiz. Şimdi GridView nesnesinin özelliklerini AutoGenerateColumns="False" ShowFooter="true" şeklinde yapalım ve aşağıdaki gibi kodlarımızı yazalım.
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 fiyatToplam = 0;
int grvFytToplam = 0;
int stokid = 0;
int satirIndex = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindList();
}
}
protected void bindList()
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\kategori.mdf;Integrated Security=True");
{
int i = 0;
con.Open();
SqlCommand cmd = new SqlCommand("select * from [dbo].[Table]", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
da.Dispose();
con.Close();
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
stokid = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "stokId").ToString());
int tmpTotal = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Fiyat").ToString());
fiyatToplam += tmpTotal;
grvFytToplam += tmpTotal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblToplam = (Label)e.Row.FindControl("lblToplam");
lblToplam.Text = grvFytToplam.ToString();
e.Row.Cells[0].Text = "Genel Toplam :";
e.Row.Font.Bold = true;
e.Row.Font.Size = 16;
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
bool yeniSatir = false;
if ((stokid > 0) && (DataBinder.Eval(e.Row.DataItem, "stokId") != null))
{
if (stokid != Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "stokId").ToString()))
yeniSatir = true;
}
if ((stokid > 0) && (DataBinder.Eval(e.Row.DataItem, "stokId") == null))
{
yeniSatir = true;
satirIndex = 0;
}
if (yeniSatir)
{
GridView GridView1 = (GridView)sender;
GridViewRow yeniToplamSatir = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
yeniToplamSatir.Font.Bold = true;
yeniToplamSatir.BackColor = System.Drawing.Color.OrangeRed;
yeniToplamSatir.ForeColor = System.Drawing.Color.White;
TableCell tblCell = new TableCell();
tblCell.Font.Size = 14;
tblCell.Text = "Ara Toplam";
tblCell.HorizontalAlign = HorizontalAlign.Left;
tblCell.ColumnSpan = 4;
yeniToplamSatir.Cells.Add(tblCell);
tblCell = new TableCell();
tblCell.HorizontalAlign = HorizontalAlign.Right;
tblCell.Text = fiyatToplam.ToString();
yeniToplamSatir.Cells.Add( tblCell);
GridView1.Controls[0].Controls.AddAt( e.Row.RowIndex + satirIndex, yeniToplamSatir);
satirIndex++;
fiyatToplam = 0;
}
}
}
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" OnRowCreated="GridView1_RowCreated" CellPadding="4" ForeColor="#333333" GridLines="None" Height="133px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="stokId" HeaderText="stokId" />
<asp:BoundField DataField="Yazar" HeaderText="Yazar" />
<asp:BoundField DataField="Kitap" HeaderText="Kitap" />
<asp:TemplateField HeaderText="Fiyat" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Label ID="lblqty" runat="server" Text='<%# Eval("Fiyat") %>' />
</ItemTemplate>
<FooterTemplate>
<div style="text-align: right;">
<asp:Label ID="lblToplam" runat="server" Font-Bold="true" />
</div>
</FooterTemplate>
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<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