Học Lập Trình

Hướng dẫn sử dụng Data BinDing trong c#

Hướng dẫn cách sử dụng Data Binding với các control listbox, combobox DataGridview

Data Binding là gì?
Cách Binding bằng Wizard

Cách dùng Data bound Control

Data Binding là một chức năng cho phép ta gắn kết nguồn dữ liệu với một control và điều khiển tự động hiển thị dữ liệu trên giao diện.
+ Cách Binding bằng Wizard
Cấu hình hiển thị dữ liệu (không viết bất kỳ dòng lệnh nào)
+ Cách dùng Data bound Control  

Listbox
Combobox
DataGridView
Cách dùng Binding Source

 Cách Binding Bằng Wizard

1 1

Chọn Data Source- Chọn Add Project Data Source- chọn database – Dataset.

2 3

Đối với hiển thị dữ liệu bằng wizard thì chỉ cần kéo thả control cần và vào properties chọn tới Databinding chọn Text chọn bản phù hợp cột ở data sql server.
3 2

Binding cho Listbox:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace binding

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

 SqlConnection conec = new SqlConnection(@”Server =DESKTOP-CR8PM8T\SQLEXPRESS; Database=CSDLSANPHAM ; User ID=sa; pwd =123″);

            SqlDataAdapter adapter = new SqlDataAdapter(“select * from SanPham”, conec);

            DataSet ds = new DataSet();

            adapter.Fill(ds);

            lstSanPham.DataSource = ds.Tables[0];

            lstSanPham.ValueMember = “Ma”;

            lstSanPham.DisplayMember = “TenSanPham”;

   

        }          

    }

}

 

Binding cho DataGridView kết hợp combobox

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace binding

{

    public partial class Form5 : Form

    {

        public Form5()

        {

            InitializeComponent();

        }

        bool finished = false;

        private void Form5_Load(object sender, EventArgs e)

        {

            SqlConnection conec = new SqlConnection(@”Server =DESKTOP-CR8PM8T\SQLEXPRESS; Database=CSDLSANPHAM ; User ID=sa; pwd =123″);

            SqlDataAdapter adapter = new SqlDataAdapter(“Select * from DanhMuc”, conec);

            DataSet ds = new DataSet();

            adapter.Fill(ds);

            finished = false;

            cboDM.DataSource = ds.Tables[0];

            cboDM.ValueMember = “MaDM”;

            cboDM.DisplayMember = “TenDM”;

            finished = true;

        }

     //hiện thị sản phẩm lên datagidview theo mã danh mục

        private void cboDM_SelectedIndexChanged(object sender, EventArgs e)

        {

            if (cboDM.SelectedIndex==-1)

            {

                return;

            }

            if (finished==false)

            {

                return;

            }

            int madm = (int)cboDM.SelectedValue;

            SqlConnection conec = new SqlConnection(@”Server =DESKTOP-CR8PM8T\SQLEXPRESS; Database=CSDLSANPHAM ; User ID=sa; pwd =123″);

 

            SqlDataAdapter adap = new SqlDataAdapter(“Select * from SanPham where MaDm=” + madm, conec);

            DataSet ds1 = new DataSet();

            adap.Fill(ds1);

            gvSanPham.DataSource = ds1.Tables[0];

        

        }

    }

}

Hướng dẫn sử dụng Data BinDing trong c#


Cách dùng Binding Source:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace binding

{

    public partial class Form3 : Form

    {

        public Form3()

        {

            InitializeComponent();

        }

        DataSet ds = null;

        SqlDataAdapter adapter = null;

        BindingSource bs = null;

      

        private void Form3_Load(object sender, EventArgs e)

        {

       SqlConnection conec = new SqlConnection(@”Server =DESKTOP-CR8PM8T\SQLEXPRESS; Database=CSDLSANPHAM ; User ID=sa; pwd =123″);

            adapter = new SqlDataAdapter(“select * from SanPham”, conec);

            SqlCommandBuilder buider = new SqlCommandBuilder(adapter);

            ds = new DataSet();

            adapter.Fill(ds, “SanPham”);

            bs = new BindingSource(ds,”SanPham”);

            txtMa.DataBindings.Add(“Text”, bs, “Ma”);

            txtTen.DataBindings.Add(“Text”, bs, “TenSanPham”);

            txtGia.DataBindings.Add(“Text”, bs, “Gia”);

            txtMaDM.DataBindings.Add(“Text”, bs, “MaDm”);

            lblPosition.Text = “/” + bs.Count;

        }

    

        private void btnFist_Click(object sender, EventArgs e)

        {

            bs.Position = 0;

            lblPosition.Text = (bs.Position+1)+”1/” + bs.Count;

        }

        private void bntPrevious_Click(object sender, EventArgs e)

        {

            if (bs.Position>0)

            {

                bs.Position–;

                lblPosition.Text = (bs.Position + 1) + “1/” + bs.Count;

            }

        }

        private void btnNex_Click(object sender, EventArgs e)

        {

            if (bs.Position < bs.Count-1)

            {

                bs.Position++;

                lblPosition.Text = (bs.Position + 1) + “1/” + bs.Count;

            }

        }

        private void btnLast_Click(object sender, EventArgs e)

        {

            bs.Position = bs.Count – 1;

            lblPosition.Text = (bs.Position + 1) + “1/” + bs.Count;

 

        }

        private void btnThem_Click(object sender, EventArgs e)

        {

            bs.AddNew();

        }

        private void btnSua_Click(object sender, EventArgs e)

        {

            bs.EndEdit();

         

            int kq = adapter.Update(ds.Tables[“SanPham”]);

            if (kq>0)

            {

                MessageBox.Show(“Lưu Thành Công”);

            }

        }

        private void btnXoa_Click(object sender, EventArgs e)

        {

            bs.RemoveCurrent();

            int kq = adapter.Update(ds.Tables[“SanPham”]);

            if (kq>0)

            {

                MessageBox.Show(“Xoa tahnh cong”);

            }

        }

    }

}

Hướng dẫn Data BinDing trong c#