|
C# อ่านค่า Text file มาเเสดงบน DataGridView |
C# อ่านค่า Text file มาเเสดงบน DataGridView อ่านค่า Textfile มาเเสดงบน DataGridView เเล้วดึงข้อมูลมาแสดงบน Textbox พอเป็นแนวทางสำหรับ คนที่ต้องการนำไปประยุกใช้ครับ
ข้อมูล Textfile ที่ผมเขียนไว้ครับ Data.txt
Mr,Danny,Welbeck,Danny,111111111,Arsenal
Mr,Pol,Pogba,Pol,22222222,ManU
Mr,Mohamad,Salah,Parasetamon,Wow20,PedDang
Mr,Harry,Kane,Kane,5555555,TOT
หน้าตาออกแบบครับ
Code (C#)
namespace ExampleReadTextfile
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.Labal1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(57, 171);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(419, 139);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(91, 39);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(139, 20);
this.textBox1.TabIndex = 1;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(311, 39);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(139, 20);
this.textBox2.TabIndex = 2;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(91, 77);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(139, 20);
this.textBox3.TabIndex = 4;
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(311, 81);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(139, 20);
this.textBox4.TabIndex = 3;
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(91, 128);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(139, 20);
this.textBox5.TabIndex = 6;
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(311, 128);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(139, 20);
this.textBox6.TabIndex = 5;
//
// Labal1
//
this.Labal1.AutoSize = true;
this.Labal1.Location = new System.Drawing.Point(31, 42);
this.Labal1.Name = "Labal1";
this.Labal1.Size = new System.Drawing.Size(27, 13);
this.Labal1.TabIndex = 7;
this.Labal1.Text = "Title";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(256, 42);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 8;
this.label2.Text = "Name";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(31, 84);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(53, 13);
this.label3.TabIndex = 9;
this.label3.Text = "Lastname";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(256, 84);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(55, 13);
this.label4.TabIndex = 10;
this.label4.Text = "Nickname";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(31, 127);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(18, 13);
this.label5.TabIndex = 11;
this.label5.Text = "tel";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(256, 131);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(36, 13);
this.label6.TabIndex = 12;
this.label6.Text = "Orther";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(526, 335);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.Labal1);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Label Labal1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
}
}
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ExampleReadTextfile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("คำนำหน้าชื่อ");
table.Columns.Add("ชื่อ");
table.Columns.Add("นามสกุล");
table.Columns.Add("ชื่อเล่น");
table.Columns.Add("เบอร์");
table.Columns.Add("อื่นๆ");
if (File.Exists(@"D:\Data.txt")) ///เช็คtext file ว่ามีอยู่ไหม
{
using (StreamReader sr = new StreamReader(@"D:\Data.txt"))
{
while (!sr.EndOfStream)
{
string[] parts = sr.ReadLine().Split(','); /// Split(',') สามารถแก้ไขตามที่ต้องการได้ เป็นเว้นวรรค # *
table.Rows.Add(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]);
}
}
}
else
{
MessageBox.Show("ไม่พบไฟล์");
}
dataGridView1.DataSource = table;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
textBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
textBox5.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
textBox6.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
}
}
}
รูปภาพ ปลากอบครับ
|
|
|
|
|
|
|
|
By : |
khing
|
|
Article : |
บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ |
|
Score Rating : |
|
|
Create Date : |
2019-04-02 |
|
Download : |
No files |
|
Sponsored Links |
|
|
|
|
|
|