Merhaba arkadaşlar bu makalemizde CSV Dosyasında arama yapacağız. Arama sonucunu GridView da göstereceğiz.
Şekil 1
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace gridview_search_data_from_csv_file
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSearch_Click(object sender, EventArgs e)
{
string searchValue = txtSearch.Text.Trim();
// Set File path. Dosya yolunu tanimliyoruz
//string csvPath = Server.MapPath("~/Files/customer.csv");
string csvPath = "D:\\customers.csv";
//Create a DataTable. Datatable olusturuyoruz
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[13] { new DataColumn("CustomerId", typeof(string)),
new DataColumn("FirstName", typeof(string)),
new DataColumn("LastName",typeof(string)),
new DataColumn("Company", typeof(string)),
new DataColumn("Address", typeof(string)),
new DataColumn("City", typeof(string)),
new DataColumn("State", typeof(string)),
new DataColumn("Country", typeof(string)),
new DataColumn("PostalCode", typeof(string)),
new DataColumn("Phone", typeof(string)),
new DataColumn("Fax", typeof(string)),
new DataColumn("Email", typeof(string)),
new DataColumn("SupportRepId", typeof(string)) });
//Read the contents of CSV file.
//CSV dosya icerigini okuyoruz.
string csvData = File.ReadAllText(csvPath);
//if (!string.IsNullOrEmpty(txtSearch.Text.Trim()))
//{
//Execute a loop over the rows.Satirlarin uzerine bir donge uygulanir.
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
bool searchValueExist = false;
//Execute a loop over the columns.Sutunlarin uzerine bir dongu uygulanir
foreach (string cell in row.Split(','))
{
bool valueExist = cell.ToUpper().Contains(searchValue.ToUpper());
if (valueExist)
{
searchValueExist = true;
break;
}
}
if (searchValueExist)
{
dt.Rows.Add();
int i = 0;
//Execute a loop over the columns. Sutunlarin uzerine bir dongu uygulanir
foreach (string cell in row.Split(','))
{
dt.Rows[dt.Rows.Count - 1][i] = cell;
i++;
}
}
}
}
//}
//Bind the DataTable Datatable i baglayin.
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
WebForm.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="gridview_search_data_from_csv_file.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtSearch" runat="server" Font-Names="Arial" Font-Size="Large" Width="184px"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Font-Names="Arial" Font-Size="Large" OnClick="btnSearch_Click" Text="Search" Width="134px" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Names="Arial" Font-Size="Large" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<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