|
C# การดึง Database จาก DataGridView มาลงใน TextBox แบบบ้าน ๆ |
C# การดึง Database จาก DataGridView มาลงใน TextBox แบบบ้าน ๆ
ต่อจากเรื่อง C# การใช้งาน DataSource Wizard กับ DataGridView (Access 2007)
ทำให้เราได้ DataTable มาใช้งานเป็นที่เรียบร้อยแล้ว มาบทความนี้ผมก็จะออกแบบ ฟอร์ม แบบบ้านๆ จาก DataTable บ้าน ๆ ที่ผมสร้างมาครับ ผมเริ่มจาก
ลากตารางจาก datasuorce มาลงในฟอร์ม

เลือก คอลัมน์ ที่ต้องการนำมาใช้งาน แล้วก็ลากมาวางลงในฟอร์ม

เรียบร้อยแล้วครับพร้อมส่งงานได้ 555


โค้ดในฟอร์มก็ประมาณนี้
Code (C#)
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void sett_Post_ProvinceBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.sett_Post_ProvinceBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.postCodeDataSet);
}
private void Form4_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'postCodeDataSet.Sett_Post_Province' table. You can move, or remove it, as needed.
this.sett_Post_ProvinceTableAdapter.Fill(this.postCodeDataSet.Sett_Post_Province);
}
}
เรามาดูการ Binding เผื่อได้ใช้ประโยชน์จากมันมั่ง
ในฟอร์ม ตรงส่วนของ pvnIDTextBox เมื่อเราลาก คอลัมน์ มาลงในฟอร์ม ก็จะมีการ Binding อย่างอัตโนมัติอยู่แล้วครับ

ส่วนของ โค้ดการ Binding ก็จะเป็นแบบนี้ครับ
Code (C#)
this.pvnIDTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.sett_Post_ProvinceBindingSource, "pvnID", true));
ลากวางๆๆๆๆ เยอะ ๆ ออกแบบดีๆซักหน่อยก็ออกมาเป็นแบบนี้ครับ



เมื่อเราลาก datasuorce มาลงในฟอร์ม หลาย ๆ table มีแค่ TableAdapter.Fill มาให้ครับ bindingNavigator จะไม่มาด้วยครับ เราก็ก๊อบมาวางเอาง่ายดีแต่ต้องอย่าลืมตั้งค่า BindingSource ให้ตรงกับที่ต้องการด้วยครับ

แค่นี้เราก็ได้ bindingNavigator ที่ใช้งานได้แล้วครับ
ส่วนการบันทึกใน database ก็แค่ก๊อบจากตัวเดิมมาลงใน event ของbindingNavigator นั้น ๆครับ แล้วก็เปลี่ยนชื่อ BindingSource
Code (C#)
//Update
this.Validate();
this.ชื่อ BindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.postCodeDataSet);
แต่เมื่อเรา save data จำต้อง refresh ข้อมูลครับ ผมจึงต้องเพิ่มโค้ดบ้านๆ อีกตัวลงไปคือ TableAdapter.Fill โค้ดบ้านๆโดยรวมจึงออกมาแบบนี้ครับ
Code (C#)
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'postCodeDataSet.Sett_Post_PostCode' table. You can move, or remove it, as needed.
this.sett_Post_PostCodeTableAdapter.Fill(this.postCodeDataSet.Sett_Post_PostCode);
// TODO: This line of code loads data into the 'postCodeDataSet.Sett_Post_PostOffice' table. You can move, or remove it, as needed.
this.sett_Post_PostOfficeTableAdapter.Fill(this.postCodeDataSet.Sett_Post_PostOffice);
// TODO: This line of code loads data into the 'postCodeDataSet.Sett_Post_Amphur' table. You can move, or remove it, as needed.
this.sett_Post_AmphurTableAdapter.Fill(this.postCodeDataSet.Sett_Post_Amphur);
// TODO: This line of code loads data into the 'postCodeDataSet.DataTable1' table. You can move, or remove it, as needed.
this.dataTable1TableAdapter.Fill(this.postCodeDataSet.DataTable1);
// TODO: This line of code loads data into the 'postCodeDataSet.Sett_Post_Province' table. You can move, or remove it, as needed.
this.sett_Post_ProvinceTableAdapter.Fill(this.postCodeDataSet.Sett_Post_Province);
}
#region _SaveData
private void toolStripButton7_Click(object sender, EventArgs e)
{
//Update PostCode
this.Validate();
this.sett_Post_PostCodeBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.postCodeDataSet);
this.sett_Post_PostCodeTableAdapter.Fill(this.postCodeDataSet.Sett_Post_PostCode);
}
private void toolStripButton14_Click(object sender, EventArgs e)
{
//Update PostCode
this.Validate();
this.sett_Post_ProvinceBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.postCodeDataSet);
this.sett_Post_ProvinceTableAdapter.Fill(this.postCodeDataSet.Sett_Post_Province);
}
private void toolStripButton21_Click(object sender, EventArgs e)
{
//Update Amphur
this.Validate();
this.sett_Post_AmphurBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.postCodeDataSet);
this.sett_Post_AmphurTableAdapter.Fill(this.postCodeDataSet.Sett_Post_Amphur);
}
private void toolStripButton28_Click(object sender, EventArgs e)
{
//Update PostOffice
this.Validate();
this.sett_Post_PostOfficeBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.postCodeDataSet);
this.sett_Post_PostOfficeTableAdapter.Fill(this.postCodeDataSet.Sett_Post_PostOffice);
}
#endregion
}
จาก C# การใช้ DataGridViewComboBoxColumn ใน DataGridView แบบบ้าน ๆ
ผมจะเอา รายชื่อจังหวัด อำเภอ ตู้ไปรณีษย์ มาทำเป็น DataGridViewComboBoxColumn ออกมาได้เป็นแบบนี้ครับ


ปรับ AlternatingRowsDefaultCellStyle ซะหน่อย
DataGridView แบบบ้าน ๆ ก็จะยิ่ง บ้านๆ หนักกว่าเดิม 5555

C# การดึง Database มาลงใน TextBox แบบบ้าน ๆ ก็ขอจบลงเพียงเท่านี้ครับ เนื่องจากใช้ความรู้แบบบ้านๆ จึงได้โค้ด และ หลักการแบบบ้านๆ มาเขียนบทความแบบบ้านๆ ให้อ่านกันครับ

|
|
|
|
 |
|
|
|
By : |
TOR_CHEMISTRY
|
|
Article : |
บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ |
|
Score Rating : |
   |
|
Create Date : |
2015-10-06 |
|
Download : |
No files |
|
Sponsored Links |
|
|
|
|
|
|