0% found this document useful (0 votes)
22 views

Tahap Membuat Table: Namespace Public Partial Class

The document discusses creating and managing a database table in C# using LINQ. It includes code to: 1) Define the database context and connect to the database. 2) Display the table data in a datagrid and allow adding, editing, and deleting records. 3) Handle the add, edit, and delete buttons to open forms, submit changes to the database, and refresh the datagrid. 4) The add and edit forms populate fields and handle submitting the record.

Uploaded by

Wulan Fitriani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Tahap Membuat Table: Namespace Public Partial Class

The document discusses creating and managing a database table in C# using LINQ. It includes code to: 1) Define the database context and connect to the database. 2) Display the table data in a datagrid and allow adding, editing, and deleting records. 3) Handle the add, edit, and delete buttons to open forms, submit changes to the database, and refresh the datagrid. 4) The add and edit forms populate fields and handle submitting the record.

Uploaded by

Wulan Fitriani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Add,edit,delete c#- ulan@copyright 2012 Tahap membuat table Tentukan Colomn Tentukan primary key Tentukan tipe data

Tentukan allow null Tentukan is identity Save

Form Utama namespace DatabaseTracing { public partial class Form1 : Form { // Data Linq DataConnCompanyDataContext db; //inisialisasi database public Form1() { InitializeComponent(); db = new DataConnCompanyDataContext(); updateDataCompany(); } //Coneksi database private void updateDataCompany() { dataGridView1.DataSource = (from p in db.TblCompanies select p).ToList(); } //TombolAdd private void AddButton_Click(object sender, EventArgs e) { Form2 Form = new Form2(); if (Form.ShowDialog() == DialogResult.OK) { db.TblCompanies.InsertOnSubmit(Form.TblCompany); db.SubmitChanges(); updateDataCompany(); }}

Add Button

Add,edit,delete c#- ulan@copyright 2012 //Tombol Edit private void EditButt_Click(object sender, EventArgs e) { //Pemilihan Index jika tipe data id = string string selectedIndex = (string)dataGridView1.CurrentRow.Cells[0].Value; int selectedIndex = (int)dataGridView1.CurrentRow.Cells[0].Value;
Edit Button

TblCompany selectedMember = (from p in db.TblCompanies where p.CompnyId == selectedIndex select p).FirstOrDefault(); //Form kedua Form2 Form = new Form2(selectedMember); if (Form.ShowDialog() == DialogResult.OK) { db.SubmitChanges(); updateDataCompany(); } } private void delButt_Click(object sender, EventArgs e) { int selectedIndex = (int) dataGridView1.CurrentRow.Cells[0].Value;

Copy dari Editbutt

TblCompany selectedMember = (from p in db.TblCompanies where p.CompnyId == selectedIndex select p).FirstOrDefault(); if(MessageBox.Show ("Are u sure to delete this data ?","Warning !",MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)== DialogResult.OK) { db.TblCompanies.DeleteOnSubmit(selectedMember); Pemilihan Index db.SubmitChanges(); updateDataCompany(); } } } }

Add,edit,delete c#- ulan@copyright 2012 Form kedua namespace DatabaseTracing { public partial class Form2 : Form { //Inisialisasi Tabel TblCompany _tblCompany; //Properties Tabel public TblCompany TblCompany { get { return _tblCompany; } } public Form2() { InitializeComponent(); this.Text = "Add New Member"; _tblCompany = new TblCompany(); } public Form2(TblCompany tblCompany) { InitializeComponent(); this.Text = "Edit Data Member"; _tblCompany = tblCompany; textBoxNama.Text = _tblCompany.CompnyName; textBoxAdd.Text = _tblCompany.CompnyAdd; textBoxMobile.Text = _tblCompany.CompnyMobile; textBoxId.Text = Convert.ToString(_tblCompany.CompnyId); textBoxId.ReadOnly = true; } private void OK_Click(object sender, EventArgs e) { _tblCompany.CompnyName = textBoxNama.Text; _tblCompany.CompnyAdd = textBoxAdd.Text; _tblCompany.CompnyMobile= textBoxMobile.Text; } } }
Untuk Edit

Untuk Add

You might also like