Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > C# (.NET) > ASP.NET CSV > (C#) ASP.NET CSV & GridView



Clound SSD Virtual Server

(C#) ASP.NET CSV & GridView

(C#) ASP.NET CSV & GridView ตัวอย่างการเขียน ASP.NET เปิดไฟล์ CSV และ Bind ข้อมูลลงใน GridView

Language Code : VB.NET || C#

Framework : 2,3,4

csv/customer.csv

CustomerID,Name,Email,CountryCode,Budget,Used
"C001","Win Weerachai","[email protected]","TH","1,000,000.00","600,000.00"
"C002","John Smith","[email protected]","EN","2,000,000.00","800,000.00"
"C003","Jame Born","[email protected]","US","3,000,000.00","600,000.00"
"C004","Chalee Angel","[email protected]","US","4,000,000.00","100,000.00"



AspNetCsvGridView.aspx

<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
	String strFields = "CustomerID";
    void Page_Load(object sender,EventArgs e)		
	{
		if(!Page.IsPostBack)
		{
			BindData();
		}
    }

	void BindData()
	{
        OleDbConnection objConn = new OleDbConnection();
		OleDbCommand objCmd = new OleDbCommand();
        OleDbDataAdapter dtAdapter = new OleDbDataAdapter();
       
		DataSet ds = new DataSet();
		String strConnString,strSQL;

		strConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath("csv/") +
		";Extended Properties='TEXT;HDR=Yes;FMT=Delimited;Format=Delimited(,)'";
		
		strSQL = "SELECT * FROM customer.csv ORDER BY " + strFields + " ASC";

		objConn.ConnectionString = strConnString;

		objCmd.Connection = objConn;
		objCmd.CommandText = strSQL;
		objCmd.CommandType = CommandType.Text;

		dtAdapter.SelectCommand = objCmd;

		dtAdapter.Fill(ds);

		//*** BindData to GridView ***//
		myGridView.DataSource = ds;
		myGridView.DataBind();
		
		dtAdapter = null;
		objConn.Close();
		objConn = null;

	}
	
	void ShowPageCommand(Object s,GridViewPageEventArgs e)
	{
		myGridView.PageIndex = e.NewPageIndex;
		BindData();
	}	

	void SortCommand(Object s,GridViewSortEventArgs e)
	{
		strFields = e.SortExpression;
		BindData();
	}


	void myGridView_RowDataBound(Object sender,GridViewRowEventArgs e) 
	{

		//*** CustomerID ***//
		Label lblCustomerID = (Label)(e.Row.FindControl("lblCustomerID"));
		if (lblCustomerID != null)
		{
			lblCustomerID.Text = (string)DataBinder.Eval(e.Row.DataItem, "CustomerID");
		}

		//*** Email ***//
		Label lblName = (Label)(e.Row.FindControl("lblName"));
		if (lblName != null)
		{
			lblName.Text = (string)DataBinder.Eval(e.Row.DataItem, "Name");
		}

		//*** Name ***//
		Label lblEmail = (Label)(e.Row.FindControl("lblEmail"));
		if (lblEmail != null)
		{
			lblEmail.Text = (string)DataBinder.Eval(e.Row.DataItem, "Email");
		}

		//*** CountryCode ***//
		Label lblCountryCode = (Label)(e.Row.FindControl("lblCountryCode"));
		if (lblCountryCode != null)
		{
			lblCountryCode.Text = (string)DataBinder.Eval(e.Row.DataItem, "CountryCode");
		}

		//*** Budget ***//
		Label lblBudget = (Label)(e.Row.FindControl("lblBudget"));
		if (lblBudget != null)
		{
			lblBudget.Text = (string)DataBinder.Eval(e.Row.DataItem, "Budget");
		}

		//*** Used ***//
		Label lblUsed = (Label)(e.Row.FindControl("lblUsed"));
		if (lblUsed != null)
		{
			lblUsed.Text = (string)DataBinder.Eval(e.Row.DataItem, "Used");
		}

	}

</script>
<html>
<head>
    <title>ThaiCreate.Com ASP.NET - GridView</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView id="myGridView" runat="server"  PageSize="2" OnSorting="SortCommand"
	OnPageIndexChanging="ShowPageCommand" AllowPaging="True"  AllowSorting="True"
	AutoGenerateColumns="False" onRowDataBound="myGridView_RowDataBound">

	<Columns>

	<asp:TemplateField SortExpression="CustomerID"  HeaderText="CustomerID">
		<ItemTemplate>
			<asp:Label id="lblCustomerID" runat="server"></asp:Label>
		</ItemTemplate>
	</asp:TemplateField>

	<asp:TemplateField SortExpression="Name" HeaderText="Name">
		<ItemTemplate>
			<asp:Label id="lblName" runat="server"></asp:Label>
		</ItemTemplate>
	</asp:TemplateField>

	<asp:TemplateField SortExpression="Email" HeaderText="Email">
		<ItemTemplate>
			<asp:Label id="lblEmail" runat="server"></asp:Label>
		</ItemTemplate>
	</asp:TemplateField>

	<asp:TemplateField SortExpression="CountryCode" HeaderText="CountryCode">
		<ItemTemplate>
			<asp:Label id="lblCountryCode" runat="server"></asp:Label>
		</ItemTemplate>
	</asp:TemplateField>

	<asp:TemplateField SortExpression="Budget" HeaderText="Budget">
		<ItemTemplate>
			<asp:Label id="lblBudget" runat="server"></asp:Label>
		</ItemTemplate>
	</asp:TemplateField>

	<asp:TemplateField SortExpression="Used" HeaderText="Used">
		<ItemTemplate>
			<asp:Label id="lblUsed" runat="server"></asp:Label>
		</ItemTemplate>
	</asp:TemplateField>

	</Columns>
</asp:GridView>
</form>
</body>
</html>



คำอธิบาย
จากตัวอย่างจะเห็นว่า ASP.NET ใช้ ADO เข้ามาจัดการอ่านไฟล์ CSV ที่อยู่ในโฟเดอร์ csv/ ซึ่งสามารถอ่านได้อยู่ในรูปแบบของ DataTable หรือ DataSet

Screenshot

ASP.NET CSV GridView






   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-11-29 22:44:20 / 2009-06-03 07:25:18
  Download : Download  (C#) ASP.NET CSV & GridView
 Sponsored Links / Related

 
(C#) ASP.NET Read CSV file
Rating :

 
(C#) ASP.NET CSV & ADO.NET - System.Data.OleDb
Rating :

 
(C#) ASP.NET CSV & ODBC - System.Data.Odbc
Rating :

 
(C#) ASP.NET Convert CSV To DataTable
Rating :

 
(C#) ASP.NET Convert CSV To DataSet
Rating :

 
(C#) ASP.NET CSV & GridView
Rating :

 
(C#) ASP.NET Write/Export to CSV file
Rating :

 
(C#) ASP.NET CSV & ADO.NET Insert Record
Rating :

 
(C#) ASP.NET Import CSV to Database
Rating :

 
(C#) ASP.NET Export Database To CSV
Rating :

 
(C#) ASP.NET Export Record/Data To CSV
Rating :

 
(C#) ASP.NET Upload & Import CSV to Database
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่