Professional Documents
Culture Documents
namespace K15CMUTCD1_NEW { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { try { //(1) To i tng 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; } } private void button1_Click(object sender, EventArgs e) { try { //Mo hinh phi ket noi //select SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Data Source=(local);Initial Catalog" + "=banhang;Integrated Security=True"; SqlDataAdapter adap = new SqlDataAdapter( "Select * from mathang", connection); DataSet dataSet = new DataSet(); adap.Fill(dataSet, "mathang"); //insert string mahang = this.textBox1.Text; string tenhang = this.textBox2.Text; string maloai = this.textBox3.Text; dataSet.Tables[0].Rows[0].SetAdded(); //insert dataset SqlCommand cmd = new SqlCommand( "insert into mathang (mahang,tenhang,maloai) " + " values( '" + mahang + "','" + tenhang + "','" + maloai + "')", connection); adap.InsertCommand = cmd; adap.Update(dataSet, "mathang"); dataSet.Clear(); adap.Fill(dataSet, "mathang"); dataGridView1.DataSource = dataSet.Tables[0]; } catch (SqlException err) { this.label4.Text = err.Message; } } private void button2_Click(object sender, EventArgs e) { try { //Mo hinh phi ket noi //select SqlConnection connection = new SqlConnection(); connection.ConnectionString = "Data Source=(local);Initial Catalog=banhang;" + "Integrated Security=True"; SqlDataAdapter adap = new SqlDataAdapter( "Select * from mathang", connection); DataSet dataSet = new DataSet(); adap.Fill(dataSet, "mathang"); //update string mahang = this.textBox1.Text; string tenhang = this.textBox2.Text; string maloai = this.textBox3.Text; dataSet.Tables[0].PrimaryKey = new DataColumn[] { dataSet.Tables[0].Columns[0] }; DataRow row = dataSet.Tables[0].Rows.Find(mahang); row.SetModified(); //update dataset SqlCommand cmd = new SqlCommand( "update mathang set " + "tenhang='" + tenhang + "'," + "maloai='" + maloai + "'" + " where mahang='" + mahang + "'", connection); adap.UpdateCommand = cmd;
adap.Update(dataSet, "mathang"); dataSet.Clear(); adap.Fill(dataSet, "mathang"); dataGridView1.DataSource = dataSet.Tables[0]; } catch (SqlException err) { this.label4.Text = err.Message; } } private void button3_Click(object sender, EventArgs e) { try { //Mo hinh phi ket noi SqlConnection connection = new SqlConnection(); connection.ConnectionString = "Data Source=(local);Initial Catalog=banhang;" + "Integrated Security=True"; SqlDataAdapter adap = new SqlDataAdapter( "Select * from mathang", connection); SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adap); DataSet dataSet = new DataSet(); adap.Fill(dataSet, "mathang"); string mahang = this.textBox1.Text; //delete dataset DataTable table = dataSet.Tables[0]; table.PrimaryKey = new DataColumn[] { dataSet.Tables[0].Columns[0] }; DataRow row = table.Rows.Find(mahang); row.Delete(); dataGridView1.DataSource = dataSet.Tables[0]; } catch (SqlException err) { this.label4.Text = err.Message; } catch (NullReferenceException er) { this.label4.Text = er.Message; } } private void button4_Click(object sender, EventArgs e) { //Tu viet thuc hien cau truy van dem so ban ghi //dataTable.Rows[0].Columns[0]; } } }
string maloai = this.comboBox1.SelectedValue.ToString(); data.Update("insert into mathang (mahang,tenhang,maloai) " + " values( '" + mahang + "','" + tenhang + "','" + maloai + "')"); } catch (SqlException err) { this.label7.Text = err.Message; } } private void button2_Click(object sender, EventArgs e) { try { string mahang = this.txtMaHang.Text; string tenhang = this.txtTenHang.Text; string maloai = this.comboBox1.SelectedValue.ToString(); data.Update("update mathang set " + "tenhang='" + tenhang + "'," + "maloai='" + maloai + "'" + " where mahang='" + mahang + "'"); } catch (SqlException err) { this.label7.Text = err.Message; } } private void button3_Click(object sender, EventArgs e) { //DialogResult n = MessageBox.Show("Ban co chac chan muon xoa khong?", // "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); DialogDeleteQuestion f = new DialogDeleteQuestion(); DialogResult n = f.ShowDialog(); if (n == DialogResult.OK) { try { string mahang = this.txtMaHang.Text; data.Update("delete from mathang where mahang='" + mahang + "'"); } catch (SqlException err) { this.label7.Text = err.Message; } } } //Thuc hien khi nguoi dung ket thuc chinh sua cell private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { try { bindingSource.EndEdit(); data.UpdateView(this.dataTable); } catch (SqlException err) { this.label7.Text = err.Message; } }
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) { //if (e.ColumnIndex == 4) //cho cot chon hien hanh // MessageBox.Show("Phai nhap so", "Li", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //else if (e.ColumnIndex ==5) // MessageBox.Show("Phai nhap ngay", "Li", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } //Thuc hien khi them hang private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { try { bindingSource.EndEdit(); data.UpdateView(this.dataTable); } catch (SqlException err) { this.label7.Text = err.Message; } } //Thuc hien khi xoa hang private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) { try { bindingSource.EndEdit(); data.UpdateView(this.dataTable); } catch (SqlException err) { this.label7.Text = err.Message; } } private void button4_Click(object sender, EventArgs e) { this.label7.Text = "So mat hang: "+ data.GetOneData("select count(*) from mathang").ToString(); } } }
FormHoaDonChiTiet2_Class
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"); this.comboBox1.DataSource = tableKhachHang; this.comboBox1.DisplayMember = "hoten"; this.comboBox1.ValueMember = "makh"; this.dataGridView2.DataSource = bindSourceHDChiTiet; this.textBox3.DataBindings.Add("Text", bindSourceHoaDon, "mahd"); this.maskedTextBox1.DataBindings.Add("Text", bindSourceHoaDon, "ngayhd", true, DataSourceUpdateMode.OnValidation, null, "MM/dd/yyyy");
this.comboBox1.DataBindings.Add("SelectedValue", bindSourceHoaDon, "makh"); this.bindingNavigator2.BindingSource = bindSourceHoaDon; ; this.bindingNavigator1.BindingSource = bindSourceHDChiTiet; this.label3.Text = "of {" + this.bindSourceHoaDon.Count + "}"; this.label4.Text = "of {" + this.bindSourceHDChiTiet.Count + "}"; } private void button3_Click(object sender, EventArgs e) { this.bindSourceHoaDon.MoveFirst(); } private void button2_Click(object sender, EventArgs e) { this.bindSourceHoaDon.MovePrevious(); } private void button1_Click(object sender, EventArgs e) { this.bindSourceHoaDon.MoveNext(); } private void button4_Click(object sender, EventArgs e) { this.bindSourceHoaDon.MoveLast(); } private void textBox1_TextChanged(object sender, EventArgs e) { try { this.bindSourceHoaDon.Position = Convert.ToInt16(this.textBox1.Text) 1; } catch (FormatException) { this.textBox1.Text = ""; } } private void button9_Click(object sender, EventArgs e) { this.bindSourceHoaDon.AddNew(); } private void button11_Click(object sender, EventArgs e) { this.bindSourceHoaDon.RemoveAt(this.bindSourceHoaDon.Position); this.label3.Text = "of {" + this.bindSourceHoaDon.Count + "}"; } private void button5_Click(object sender, EventArgs e) { this.bindSourceHDChiTiet.MoveFirst(); } private void button6_Click(object sender, EventArgs e) { this.bindSourceHDChiTiet.MovePrevious(); }
private void button7_Click(object sender, EventArgs e) { this.bindSourceHDChiTiet.MoveNext(); } private void button8_Click(object sender, EventArgs e) { this.bindSourceHDChiTiet.MoveLast(); } private void button10_Click(object sender, EventArgs e) { this.bindSourceHDChiTiet.AddNew(); } private void button12_Click(object sender, EventArgs e) { this.bindSourceHDChiTiet.RemoveAt(this.bindSourceHDChiTiet.Position); this.label4.Text = "of {" + this.bindSourceHDChiTiet.Count + "}"; } private void textBox2_TextChanged(object sender, EventArgs e) { try { this.bindSourceHDChiTiet.Position = Convert.ToInt16(this.textBox2.Text) - 1; } catch (FormatException) { this.textBox2.Text = ""; } } private void button13_Click(object sender, EventArgs e) { try { bindSourceHoaDon.EndEdit(); data.UpdateView1(dataSet.Tables["hoadon"]); } catch (SqlException err) { this.label7.Text = err.Message; } } private void button14_Click(object sender, EventArgs e) { try { this.bindSourceHDChiTiet.EndEdit(); data.UpdateView2(dataSet.Tables["hdchitiet"]); } catch (SqlException err) { this.label7.Text = err.Message; } } private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
10
this.dateTimePicker1.CustomFormat = "MM/dd/yyyy"; this.dateTimePicker1.Format = DateTimePickerFormat.Custom; this.maskedTextBox1.Text = this.dateTimePicker1.Text; } private void toolStripButton2_Click(object sender, EventArgs e) { try { bindSourceHoaDon.EndEdit(); data.UpdateView1(dataSet.Tables["hdchitiet"]); } catch (SqlException err) { this.label7.Text = err.Message; } } private void toolStripButton1_Click(object sender, EventArgs e) { try { bindSourceHDChiTiet.EndEdit(); data.UpdateView2(dataSet.Tables["hoadon"]); } catch (SqlException err) { this.label7.Text = err.Message; }}}}
11
FormTimTheoLoai
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) To i tng 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='" // + maloai + "'", strcon); //Cach 2 su dung loc bang thuoc tinh RowFilter
12
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; } } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {}}}
FormTimTheoTen
using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; 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); DataTable table = new DataTable(); dataAdapter.Fill(table); this.dataGridView1.DataSource = table;
13
} catch (SqlException err) { this.label2.Text = err.Message; } } private void FormTimTheoTen_Load(object sender, EventArgs e) { } } }
FormTreeViewMatHang
using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms;
namespace ImageThumbnails { public partial class TreeViewMatHang : Form { clsDataBase data = new clsDataBase(); public TreeViewMatHang() { InitializeComponent(); } private void TreeViewMatHang_Load(object sender, EventArgs e) { FillTree(); } 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) { DataTable tableMatHang = data.GetData1("Select * from mathang where maloai ='" + maloai + "'");
14
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.textBo x1.Text.ToUpper())){ if (this.dataGridView1[1,i].Value.ToString().ToUpper().StartsWith(this.textBox1.Text.ToUp per())) { this.dataGridView1.Rows[i].Selected = true; this.dataGridView1.CurrentCell = dataGridView1[1,i]; break; } } } } }
15
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 Load() { DataTable table = data.GetData1("select * from loaihang"); ComboBox cb = (ComboBox)f.Controls["comboBox1"]; cb.DataSource = table; cb.DisplayMember = "tenloai"; cb.ValueMember = "maloai"; dataTable = data.GetData("select * from mathang"); bindingSource = new BindingSource(); bindingSource.DataSource = dataTable; ((BindingNavigator)f.Controls["bindingNavigator1"]).BindingSource = bindingSource; DataGridView grid = (DataGridView)f.Controls["dataGridView1"]; grid.DataSource = bindingSource; grid.Columns["mahang"].ToolTipText = "Ma hang khong the chinh sua"; grid.Columns["tenhang"].SortMode = DataGridViewColumnSortMode.Automatic; //Binding d liu trong bindingSource vi cc textbox f.Controls["txtMaHang"].DataBindings.Add("Text", bindingSource, "mahang"); f.Controls["txtTenHang"].DataBindings.Add("Text", bindingSource, "tenhang"); f.Controls["txtMoTa"].DataBindings.Add("Text", bindingSource, "mota"); f.Controls["txtHinh"].DataBindings.Add("Text", bindingSource, "hinh"); f.Controls["pictureBox1"].DataBindings.Add("ImageLocation", bindingSource, "hinh"); cb.DataBindings.Add("SelectedValue", bindingSource, "maloai"); } public void Insert() {
16
try { string mahang = f.Controls["txtMaHang"].Text; string tenhang = f.Controls["txtTenHang"].Text; string maloai = ((ComboBox)f.Controls["comboBox1"]).SelectedValue.ToString(); data.Update("insert into mathang (mahang,tenhang,maloai) " + " values( '" + mahang + "','" + tenhang + "','" + maloai + "')"); } catch (SqlException err) { MessageBox.Show(err.Message); } } public void Update() { try { string mahang = f.Controls["txtMaHang"].Text; string tenhang = f.Controls["txtTenHang"].Text; string maloai = ((ComboBox)f.Controls["comboBox1"]).SelectedValue.ToString(); data.Update("update mathang set " + "tenhang='" + tenhang + "'," + "maloai='" + maloai + "'" + " where mahang='" + mahang + "'"); } catch (SqlException err) { MessageBox.Show(err.Message); } } 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); } } } //Thuc hien khi nguoi dung ket thuc chinh sua cell public void CellEndEdit() { try { bindingSource.EndEdit(); data.UpdateView(this.dataTable);
17
} 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", "Li", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //else if (col ==5) // MessageBox.Show("Phai nhap ngay", "Li", 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 using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Data.SqlClient;
namespace K15CMUTCD1_NEW { public partial class FormDataGridViewAdvanced : Form { clsDataBase data = new clsDataBase(); DataTable tableLoaiHang; DataTable tableMatHang; BindingSource bindMatHang; public FormDataGridViewAdvanced() { InitializeComponent(); } private void FormDataGridViewAdvanced_Load(object sender, EventArgs e) { //this.dataGridView1.Columns["colmahang"].Width = 75; this.colmahang.Width = 75; this.colmota.Width = 80; this.colchitiet.Width = 65; this.colmore.Width = 65; tableLoaiHang = data.GetData1("select * from loaihang"); tableMatHang = data.GetData("select * from mathang"); bindMatHang = new BindingSource(); this.dataGridView1.AutoGenerateColumns = false; bindMatHang.DataSource = tableMatHang; this.dataGridView1.DataSource = bindMatHang; this.colmahang.DataPropertyName = "mahang"; this.coltenhang.DataPropertyName ="tenhang"; this.colmota.DataPropertyName = "mota"; this.colloaihang.DataSource = tableLoaiHang; this.colloaihang.DisplayMember = "tenloai"; this.colloaihang.ValueMember = "maloai"; this.colloaihang.DataPropertyName = "maloai"; //Neu truong hinh la truong hinh kieu image co the rang buoc nhu sau //this.colhinh.DataPropertyName = "hinh"; //Neu truong hinh kieu text varchar(50) for (int i = 0; i < tableMatHang.Rows.Count; i++) { try { //Doc hinh anh this.dataGridView1.Rows[i].Height = 50;
19
string image = (string)tableMatHang.Rows[i]["hinh"]; DataGridViewImageCell cell = (DataGridViewImageCell)this.dataGridView1.Rows[i].Cells["colhinh"]; //DataGridViewImageCell cell = //(DataGridViewImageCell)this.dataGridView1["colhinh", i]; this.loadImage(cell, image); } catch (InvalidCastException) { } } } private void loadImage(DataGridViewImageCell cell, string image) { try { cell.Value = Image.FromFile(image); } catch (System.IO.FileNotFoundException) { cell.Value = null; } catch (System.ArgumentException) { cell.Value = null; } } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { //cot More... if (e.ColumnIndex == 5 || e.ColumnIndex == 6) { string mahang = tableMatHang.Rows[e.RowIndex]["mahang"].ToString(); FormDataGridViewAdvanced_More f = new FormDataGridViewAdvanced_More(); f.nhap(mahang); DialogResult n = f.ShowDialog(); if (n == DialogResult.OK) { DataGridViewButtonCell cell = (DataGridViewButtonCell)this.dataGridView1["colchitiet", e.RowIndex]; tableMatHang.Rows[e.RowIndex]["chitiet"] = f.ChiTiet; bindMatHang.EndEdit(); data.UpdateView(tableMatHang); } } } private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { Console.WriteLine("cell end edit"); try { bindMatHang.EndEdit(); data.UpdateView(tableMatHang); } catch (SqlException err)
20
{ MessageBox.Show(err.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { try { bindMatHang.EndEdit(); data.UpdateView(tableMatHang); } catch (SqlException err) { MessageBox.Show(err.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) { try { bindMatHang.EndEdit(); data.UpdateView(tableMatHang); } catch (SqlException err) { MessageBox.Show(err.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } }
21
FromDataGridViewAdvanced_More
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(); } private void FormDataGridViewAdvanced_More_Load(object sender, EventArgs e) { DataTable table = data.GetData( "select chitiet from mathang where mahang ='"+mahang+"'"); this.textBox1.Text = table.Rows[0][0].ToString(); } public void nhap(string mahang) { this.mahang = mahang; } private void button1_Click(object sender, EventArgs e) { this.chitiet = this.textBox1.Text; } } }
22