Merhaba arkadaşlar bu makalemizde GridView nesnesinde seçili CheckBox taki satırları alarak dinamik olarak oluşturacağımız diğer bir GridView nesnesinde gösterimini sağlayacağız.
Şimdi GridView nesnesine CheckBox ekleyeceğiz. Gridview nesnesinin Columns kısmına,
TemplateField ekleyeceğiz. TemplateField içindeki ItemTemplate içine CheckBox ı yerleştiriyoruz.
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkboxSelect" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
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
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void Bind()
{
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 * From [dbo].[Personel]";
komut.Connection = baglan;
baglan.Open();
komut.ExecuteNonQuery();
da.Fill(ds);
baglan.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void btnSelect_Click(object sender, EventArgs e)
{
GridView gView = new GridView();
DataTable dt = new DataTable();
int id = 1;
//datatable da 5 sutun oluşturuyoruz.
dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Surname"), new DataColumn("Department"), new DataColumn("Mail") });
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkSatir = (row.Cells[0].FindControl("chkboxSelect") as CheckBox);
if (chkSatir.Checked)
{
//datatable a secili cbox satirlarini ekliyoruz.
dt.Rows.Add(id++, row.Cells[1].Text, row.Cells[2].Text, row.Cells[3].Text, row.Cells[4].Text);
}
}
}
gView.ID = "gView";
gView.DataSource = dt;
gView.DataBind();
Form.Controls.Add(gView);
}
}
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" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkboxSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Ad" DataField="Ad" />
<asp:BoundField HeaderText="Soyad" DataField="Soyad" />
<asp:BoundField HeaderText="Departman" DataField="Kısım" />
<asp:BoundField HeaderText="Mail" DataField="Mail" />
</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>
<br />
<asp:Button ID="btnSelect" runat="server" OnClick="btnSelect_Click" Text="Get Selected" />
<br />
<br />
</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