Windows Form ตอนที่ 4 : Add Data (LINQ, Entity Framework) |
Windows Form ตอนที่ 4 : Add Data (LINQ, Entity Framework) ในตัวอย่างที่ 4 จะเป็นการออกแบบ Form สำหรับ Input ข้อมูล และ Add Entity ด้วยการรับค่าที่ Input ไปจัดเก็บลงใน Table ผ่าน Model Entities โดยในตัวอย่างนี้จะสร้าง Form ขึ้นมา 2 Form คือ Form แรกใช้แสดงข้อมูลรายการทั้งหมด ส่วน Form ที่สองจะเป็น Form สำหรับการ Input ข้อมูล โดยรับข้อมูลจากการ Input นำไป Insert ลงใน Table ผ่านคำสั่งของ LINQ to Entities

Example : การใช้งาน LINQ to Entities เพื่อรับค่าจาก Form แล้ว Insert ข้อมูลลงใน Table
frmMain ใช้สำหรับแสดงข้อมูลทั้งหมดบน DataGridView

frmAdd เป็น Form สำหรับ Input และ Insert ข้อมูลใหม่

Code frmMain.cs (C#)
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
BindData();
}
public void BindData()
{
// Create new entities Object
using (var db = new myDatabaseEntities())
{
// Get data from CUSTOMER
var ds = (from c in db.CUSTOMER
select new
{
c.CUSTOMER_ID,
c.NAME,
c.EMAIL,
c.COUNTRY_CODE,
c.BUDGET,
c.USED
}).ToList();
// Assign to DataGridView
if (ds.Count > 0)
{
// New DataSource
this.myDataGridView.AutoGenerateColumns = false;
this.myDataGridView.AllowUserToAddRows = false;
this.myDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.myDataGridView.DataSource = ds;
}
// Summary Budget Column
decimal iSumBudget = 0;
foreach (DataGridViewRow row in this.myDataGridView.Rows)
{
iSumBudget = iSumBudget + Convert.ToDecimal(row.Cells["BUDGET"].Value);
}
this.lblTotalBudget.Text = String.Format("Total Budget : {0}", iSumBudget.ToString("#,###.00"));
// Summary Used Column
decimal iSumUsed = 0;
foreach (DataGridViewRow row in this.myDataGridView.Rows)
{
iSumUsed = iSumUsed + Convert.ToDecimal(row.Cells["USED"].Value);
}
this.lblTotalUsed.Text = String.Format("Total Used : {0}", iSumUsed.ToString("#,###.00"));
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
var form = new frmAdd();
form.ShowDialog();
BindData();
}
}

Code frmAdd.cs (C#)
public partial class frmAdd : Form
{
public frmAdd()
{
InitializeComponent();
}
private void frmDetail_Load(object sender, EventArgs e)
{
this.txtCustomerID.Text = string.Empty;
this.txtName.Text = string.Empty;
this.txtEmail.Text = string.Empty;
this.txtCountryCode.Text = string.Empty;
this.txtBudget.Text = string.Empty;
this.txtUsed.Text = string.Empty;
}
private void btnSave_Click(object sender, EventArgs e)
{
using (var db = new myDatabaseEntities())
{
// Get Param
string strCustomerID = this.txtCustomerID.Text;
string strName = this.txtName.Text;
string strEmail = this.txtEmail.Text;
string strCountryCode = this.txtCountryCode.Text;
decimal strBudget = Convert.ToDecimal(this.txtBudget.Text);
decimal strUsed = Convert.ToDecimal(this.txtUsed.Text);
// Insert data to CUSTOMER
var ds = (from c in db.CUSTOMER
where c.CUSTOMER_ID == strCustomerID
select c).FirstOrDefault();
// Insert
if (ds != null)
{
MessageBox.Show(string.Format("Customer {0} Exists in table.", strCustomerID));
}
else
{
db.CUSTOMER.Add(new CUSTOMER()
{
CUSTOMER_ID = strCustomerID,
NAME = strName,
EMAIL = strEmail,
COUNTRY_CODE = strCountryCode,
BUDGET = strBudget,
USED = strUsed,
});
db.SaveChanges();
this.Close();
}
}
}
}
Screenshot

แสดงรายการข้อมูลทั้งหมดใน DataGridView ให้คลิกที่ Add เพื่อทำการเพิ่มข้อมูล

แสดง Form ซึ่งเป็น Dialog สำหรับการ Input ข้อมูล ในตัวอย่างนี้จะมีการตรวจสอบข้อมูลเช่น มี Key ซ้ำหรือไม่

จะต้องทำการกรอกข้อมูลให้ถูกต้อง

ข้อมูลที่เพิ่มลงใน Table และแสดงผลบน DataGridView ด้วยการ Bind ข้อมูลใหม่หลังจากที่ได้ Insert เรียบร้อยแล้ว
Code frmMain.vb (VB.Net)
Public Class frmMain
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BindData()
End Sub
Public Sub BindData()
' Create new entities Object
Using db = New myDatabaseEntities()
' Get data from CUSTOMER
Dim ds = (From c In db.CUSTOMER
Select New With { _
c.CUSTOMER_ID, _
c.NAME, _
c.EMAIL, _
c.COUNTRY_CODE, _
c.BUDGET, _
c.USED _
}).ToList()
' Assign to DataGridView
If ds.Count > 0 Then
' New DataSource
Me.myDataGridView.AutoGenerateColumns = False
Me.myDataGridView.AllowUserToAddRows = False
Me.myDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Me.myDataGridView.DataSource = ds
End If
' Summary Budget Column
Dim iSumBudget As Decimal = 0
For Each row As DataGridViewRow In Me.myDataGridView.Rows
iSumBudget = iSumBudget + Convert.ToDecimal(row.Cells("BUDGET").Value)
Next
Me.lblTotalBudget.Text = String.Format("Total Budget : {0}", iSumBudget.ToString("#,###.00"))
' Summary Used Column
Dim iSumUsed As Decimal = 0
For Each row As DataGridViewRow In Me.myDataGridView.Rows
iSumUsed = iSumUsed + Convert.ToDecimal(row.Cells("USED").Value)
Next
Me.lblTotalUsed.Text = String.Format("Total Used : {0}", iSumUsed.ToString("#,###.00"))
End Using
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim form = New frmAdd()
form.ShowDialog()
BindData()
End Sub
End Class
Code frmAdd.vb (VB.Net)
Public Class frmAdd
Private Sub frmAdd_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.txtCustomerID.Text = String.Empty
Me.txtName.Text = String.Empty
Me.txtEmail.Text = String.Empty
Me.txtCountryCode.Text = String.Empty
Me.txtBudget.Text = String.Empty
Me.txtUsed.Text = String.Empty
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Using db = New myDatabaseEntities()
' Get Param
Dim strCustomerID As String = Me.txtCustomerID.Text
Dim strName As String = Me.txtName.Text
Dim strEmail As String = Me.txtEmail.Text
Dim strCountryCode As String = Me.txtCountryCode.Text
Dim strBudget As Decimal = Convert.ToDecimal(Me.txtBudget.Text)
Dim strUsed As Decimal = Convert.ToDecimal(Me.txtUsed.Text)
' Insert data to CUSTOMER
Dim ds = (From c In db.CUSTOMER
Where c.CUSTOMER_ID = strCustomerID
Select c).FirstOrDefault()
' Insert
If ds IsNot Nothing Then
MessageBox.Show(String.Format("Customer {0} Exists in table.", strCustomerID))
Else
db.CUSTOMER.Add(New CUSTOMER() With { _
.CUSTOMER_ID = strCustomerID, _
.NAME = strName, _
.EMAIL = strEmail, _
.COUNTRY_CODE = strCountryCode, _
.BUDGET = strBudget, _
.USED = strUsed _
})
db.SaveChanges()
Me.Close()
End If
End Using
End Sub
End Class
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
   |
|
|
Create/Update Date : |
2015-10-02 21:20:20 /
2017-03-15 14:40:18 |
|
Download : |
No files |
|
Sponsored Links / Related |
|
|
|
|
|