GridView Nesnesine Boş Satır Ekleme
Merhaba arkadaşlar bu makalemizde GridView nesnesinde veritabanında ki verileri göstereceğiz. ProducId ye göre kategorilendirme yapacağız ve kategorilendirirken aralara boş satır ekleyeceğiz.
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 id = 0;
int satirIndex = 1;
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)
{
id = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ProductId").ToString());
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
bool yeniSatir = false;
if ((id > 0) && (DataBinder.Eval(e.Row.DataItem, "ProductId") != null))
{
if (id != Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ProductId").ToString()))
yeniSatir = true;
}
if ((id > 0) && (DataBinder.Eval(e.Row.DataItem, "ProductId") == null))
{
yeniSatir = true;
satirIndex = 0;
}
if (yeniSatir)
{
yeniSatirEkle(sender, e);
}
}
public void yeniSatirEkle(object sender, GridViewRowEventArgs e)
{
GridView GridView1 = (GridView)sender;
GridViewRow yeniToplamSatir = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
yeniToplamSatir.Font.Bold = true;
yeniToplamSatir.BackColor = System.Drawing.Color.OrangeRed;
TableCell HeaderCell = new TableCell();
HeaderCell.Height = 15;
HeaderCell.HorizontalAlign = HorizontalAlign.Center ;
HeaderCell.ColumnSpan = 4;
HeaderCell.Text = ".:::BAHADIR SAHIN:::.";
HeaderCell.ForeColor = System.Drawing.Color.White;
yeniToplamSatir.Cells.Add(HeaderCell);
GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + satirIndex, yeniToplamSatir);
satirIndex++;
}
}
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" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated" runat="server" 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:BoundField DataField="Quantity" HeaderText="Quantity" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
</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