GridView’daki Verileri
Text Dosya’ya Aktarmak
Merhaba arkadaşlar. Bu
makalemizde GridView sütunlardaki
verileri text dosya ya aktaracağız. GridView nesnemizin AutoGenerateColumns özelliğini
AutoGenerateColumns="False"
yapın.
Columns kısmına sütunlarınızı ekleyin.
Screenshot
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
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)
{
veriAl();
}
}
protected void veriAl()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(Int32));
dt.Columns.Add("Ad", typeof(string));
dt.Columns.Add("Soyad", typeof(string));
dt.Columns.Add("Mail", typeof(string));
DataRow row = dt.NewRow();
row["Id"] = 1;
row["Ad"] = "Bahadır";
row["Soyad"] = "Şahin";
row["Mail"] = "bahadirs@hotmail.com";
dt.Rows.Add(row);
row = dt.NewRow();
row["Id"] = 2;
row["Ad"] = "Fatih";
row["Soyad"] = "KOÇ";
row["Mail"] = "fatihk@hotmail.com";
dt.Rows.Add(row);
row = dt.NewRow();
row["Id"] = 3;
row["Ad"] = "Haluk";
row["Soyad"] = "AKMAN";
row["Mail"] = "haluka@hotmail.com";
dt.Rows.Add(row);
row = dt.NewRow();
row["Id"] = 4;
row["Ad"] = "Adem";
row["Soyad"] = "ALPTEKİN";
row["Mail"] = "adema@hotmail.com";
dt.Rows.Add(row);
row = dt.NewRow();
row["Id"] = 5;
row["Ad"] = "Mesut";
row["Soyad"] = "ALP";
row["Mail"] = "mesuta@hotmail.com";
dt.Rows.Add(row);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void btnTextAktar_Click(object sender, EventArgs e)
{
veriAl();
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment;
filename={0}", "test.txt"));
Response.ContentType = "application/text";
StringBuilder str = new StringBuilder();
for (int i = 0; i < GridView1.Columns.Count; i++)
{
//gview
daki sütun başlıklarını alıyoruz.
str.Append(GridView1.Columns[i].HeaderText + " " );
}
str.Append("\n");
for (int j = 0; j < GridView1.Rows.Count; j++)
{
str.Append(Environment.NewLine);
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//satırdaki
bilgileri alıyoruz.
str.Append(GridView1.Rows[j].Cells[k].Text + ',');
}
str.Append("\n");
}
Response.Write(str.ToString());
Response.End();
}
}
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>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
GridLines="None" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Ad" HeaderText="Ad" />
<asp:BoundField DataField="Soyad" HeaderText="Soyad" />
<asp:BoundField DataField="Mail" HeaderText="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="btnTextAktar" runat="server" OnClick="btnTextAktar_Click" Text="Text Dosya'ya Aktar" Width="171px" />
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede
görüşmek üzere. Bahadır ŞAHİN