FormCapNhap-PhiKetNoi using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace K15CMUTCD1_NEW { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { try { //(1) Tạo đối tượng SqlDataAdapter string strcon = "Data Source=(local);Initial Catalog=banhang;" + "Integrated Security=True"; //string strcon = @"Data Source=(local);AttachDbFilename='D:\VI DU\viduC#-moinhat\K15CMUTCD1-NEW\K15CMUTCD1-NEW\bhang.mdf';Integrated Security=True"; //string path = Application.StartupPath + @"\bhang.mdf"; //string strcon = @"Data Source=(local);AttachDbFilename="+path+";Integrated Security=True"; SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from mathang", strcon); //(2) Create a data set. DataSet dataSet = new DataSet(); //(3) Fill the data set. dataAdapter.Fill(dataSet, "mathang"); //DataTable dataTable = new DataTable(); //dataAdapter.Fill(dataTable); //(4) Display the table in a data grid using data binding. dataGridView1.DataSource = dataSet.Tables["mathang"]; //dataGridView1.DataSource = dataTable; } catch (SqlException err) { this.label4.Text = err.Message; 1
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
FormCapNhap-PhiKetNoi
using System;using System.Collections.Generic; using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;
namespace K15CMUTCD1_NEW{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { try { //(1) Tạo đố7i tượng SqlDataAdapter string strcon = "Data Source=(local);Initial Catalog=banhang;" + "Integrated Security=True"; //string strcon = @"Data Source=(local);AttachDbFilename='D:\VI DU\viduC#-moinhat\K15CMUTCD1-NEW\K15CMUTCD1-NEW\bhang.mdf';Integrated Security=True"; //string path = Application.StartupPath + @"\bhang.mdf"; //string strcon = @"Data Source=(local);AttachDbFilename="+path+";Integrated Security=True"; SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from mathang", strcon); //(2) Create a data set. DataSet dataSet = new DataSet(); //(3) Fill the data set. dataAdapter.Fill(dataSet, "mathang"); //DataTable dataTable = new DataTable(); //dataAdapter.Fill(dataTable); //(4) Display the table in a data grid using data binding. dataGridView1.DataSource = dataSet.Tables["mathang"]; //dataGridView1.DataSource = dataTable;
FormDuyet2_class(duyệt bản ghi bởi Binding Navigator, bắt sự kiện trên dataGrid sử dụng lớp cls_Database)
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace K15CMUTCD1_NEW{ public partial class FormDuyet2_class : Form { DataTable dataTable = new DataTable(); clsDataBase data = new clsDataBase(); BindingSource bindingSource;
public FormDuyet2_class() { InitializeComponent(); }
using System;using System.Collections.Generic;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;
namespace K15CMUTCD1_NEW{
public partial class FormHoaDonHDChiTiet2_class : Form { clsDataBase data = new clsDataBase(); DataSet dataSet; BindingSource bindSourceHDChiTiet; BindingSource bindSourceHoaDon; DataTable tableHoaDon;
public FormHoaDonHDChiTiet2_class() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { dataSet = new DataSet("hoadonbanhang"); DataTable tableKhachHang = data.GetData1("select * from khachhang"); tableHoaDon = data.GetData1("select * from hoadon"); DataTable tableHDChiTiet = data.GetData2("select * from hdchitiet"); dataSet.Tables.Add(tableHoaDon); dataSet.Tables.Add(tableHDChiTiet); dataSet.Tables[0].TableName = "hoadon"; dataSet.Tables[1].TableName = "hdchitiet"; dataSet.Relations.Add(new DataRelation("HOADON_HDCHITIET", tableHoaDon.Columns["mahd"], tableHDChiTiet.Columns["mahd"])); bindSourceHoaDon = new BindingSource(dataSet, "hoadon"); bindSourceHDChiTiet = new BindingSource(bindSourceHoaDon,"HOADON_HDCHITIET");
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace K15CMUTCD1_NEW{ public partial class FormTimTheoLoai : Form { public FormTimTheoLoai() { InitializeComponent(); }
private void FormTimTheoLoai_Load(object sender, EventArgs e) { try { //(1) Tạo đố7i tượng SqlDataAdapter string strcon = "Data Source=(local);Initial Catalog=banhang;" + "Integrated Security=True"; SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from loaihang", strcon); //(2) Create a data set. DataTable table= new DataTable(); //(3) Fill the datatable dataAdapter.Fill(table); //(4) Display the table in a data grid using data binding. this.comboBox1.DataSource = table; this.comboBox1.DisplayMember = "tenloai"; this.comboBox1.ValueMember = "maloai"; this.comboBox1.SelectedIndex = 1; this.comboBox1.SelectedIndex = 0; } catch (SqlException err) { this.label2.Text = err.Message; } }
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { try { string maloai = this.comboBox1.SelectedValue.ToString(); string strcon = "Data Source=(local);Initial Catalog=banhang;" + "Integrated Security=True"; //Cach 1 su dung truy van co dieu kien //SqlDataAdapter dataAdapter = // new SqlDataAdapter("select * from mathang where maloai='"
12
// + maloai + "'", strcon); //Cach 2 su dung loc bang thuoc tinh RowFilter SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from mathang", strcon); DataTable table = new DataTable(); dataAdapter.Fill(table); table.DefaultView.RowFilter = "maloai ='"+maloai+"'"; table.DefaultView.Sort = "tenhang"; this.dataGridView1.DataSource = table; } catch (SqlException err) { this.label2.Text = err.Message; } }
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;
namespace K15CMUTCD1_NEW{ public partial class FormTimTheoTen : Form { public FormTimTheoTen() { InitializeComponent(); }
private void textBox1_TextChanged(object sender, EventArgs e) { try { string tenhang = this.textBox1.Text; string strcon = "Data Source=(local);Initial Catalog=banhang;" + "Integrated Security=True"; //SQL Server cung cap ky tu thay the //ky tu % thay cho 0 hay nhieu ky tu //ky tu _ thay cho 1 ky tu //'[abc] nghia mot trong cac ky a hay b hay c string sql = "select * from mathang where tenhang " + " like '" + tenhang + "%'"; //string sql = "select * from mathang where tenhang " // + " like '" + tenhang + "_'"; //string sql = "select * from mathang where tenhang " // +" like '[AGB]" + tenhang + "'";
SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, strcon);
void FillTree() { DataTable tableLoaiHang = data.GetData("Select * from loaihang"); this.treeView1.Nodes.Clear(); foreach (DataRow row in tableLoaiHang.Rows) { TreeNode node = new TreeNode(); node.Text = row["tenloai"].ToString(); node.Tag = row["maloai"].ToString(); node.ToolTipText = "Click to get Child"; treeView1.Nodes.Add(node); FillChild(node, node.Tag.ToString()); } }
public void FillChild(TreeNode parent, string maloai)
14
{ DataTable tableMatHang = data.GetData1("Select * from mathang where maloai ='" + maloai + "'"); parent.Nodes.Clear();
foreach (DataRow row in tableMatHang.Rows) { TreeNode child = new TreeNode(); child.Text = row["tenhang"].ToString().Trim(); child.Tag = row["mahang"].ToString().Trim(); child.ToolTipText = "Click to get Child"; parent.Nodes.Add(child); } }
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node.Level == 0) //node loai hang { DataTable tableMatHang = data.GetData1("Select * from mathang where maloai ='" + e.Node.Tag + "'"); dataGridView1.DataSource = tableMatHang; } else //node mathang { DataTable tableMatHang = data.GetData1("Select * from mathang where mahang ='" + e.Node.Tag + "'"); dataGridView1.DataSource = tableMatHang; } }
private void textBox1_TextChanged(object sender, EventArgs e) { for (int i = 0;i<this.dataGridView1.Rows.Count-1;i++){ //if (this.dataGridView1.Rows[i].Cells[1].Value.ToString().ToUpper().StartsWith(this.textBox1.Text.ToUpper())){ if (this.dataGridView1[1,i].Value.ToString().ToUpper().StartsWith(this.textBox1.Text.ToUpper())) { this.dataGridView1.Rows[i].Selected = true; this.dataGridView1.CurrentCell = dataGridView1[1,i]; break; } } } }}
15
clsFormDuyet2(Tách lớp xử lý ra khỏi form)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;using System.Windows.Forms;
namespace K15CMUTCD1_NEW{ public class clsFormDuyet2 { DataTable dataTable = new DataTable(); clsDataBase data = new clsDataBase(); BindingSource bindingSource; FormDuyet2_3class f;
public clsFormDuyet2(FormDuyet2_3class f) { this.f = f; }
public void Delete() { //DialogResult n = MessageBox.Show("Ban co chac chan muon xoa khong?", // "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); DialogDeleteQuestion dialog = new DialogDeleteQuestion(); DialogResult n = dialog.ShowDialog(); if (n == DialogResult.OK) { try { string mahang = f.Controls["txtMaHang"].Text; data.Update("delete from mathang where mahang='" + mahang + "'"); } catch (SqlException err) { MessageBox.Show(err.Message); } }
17
} //Thuc hien khi nguoi dung ket thuc chinh sua cell public void CellEndEdit() { try { bindingSource.EndEdit(); data.UpdateView(this.dataTable); } catch (SqlException err) { MessageBox.Show(err.Message); } } //Thuc hien khi nhap du lieu khong trung kieu public void DataError(int col) { //if (col == 4) //cho cot chon hien hanh // MessageBox.Show("Phai nhap so", "Lố[i", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //else if (col ==5) // MessageBox.Show("Phai nhap ngay", "Lố[i", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } //Thuc hien khi them hang public void RowsAdded() { try { bindingSource.EndEdit(); data.UpdateView(this.dataTable); } catch (SqlException err) { MessageBox.Show(err.Message); } } //Thuc hien khi xoa hang public void RowsRemoved() { try { bindingSource.EndEdit(); data.UpdateView(this.dataTable); } catch (SqlException err) { MessageBox.Show(err.Message); } } //Thuc hien dem so mat hang public void Count() { f.Controls["label7"].Text = "So mat hang: "+ data.GetOneData("select count(*) from mathang").ToString(); } }}
18
FormDataGridViewAdvandced
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;
namespace K15CMUTCD1_NEW{ public partial class FormDataGridViewAdvanced : Form { clsDataBase data = new clsDataBase(); DataTable tableLoaiHang; DataTable tableMatHang; BindingSource bindMatHang;
namespace K15CMUTCD1_NEW{ public partial class FormDataGridViewAdvanced_More : Form { private string mahang; private string chitiet; public string ChiTiet { get { return chitiet; } }
clsDataBase data = new clsDataBase();
public FormDataGridViewAdvanced_More() { InitializeComponent(); }