BindingNavigator操作DatagridView的資料

來源:互聯網
上載者:User

標籤:public   using   cut   log   style   query   one   nec   blog   

1、往表單拉一個BindingNavigator:綠色框,就是一個資料導覽列

  再拉一個DataGridView,顯示資料,我添加了三列,對應要顯示的三列資料

  再拉一個BindingSource,作為上面兩個的媒人

 

資料庫資料如下:

代碼如下:

 

namespace gjjyOffline{    public partial class fenye : Form    {        public fenye()        {            InitializeComponent();        }        private void fenye_Load(object sender, EventArgs e)        {            //載入顯示資料            using (SQLiteConnection con = new SQLiteConnection("Data Source這一串"))            {                con.Open();                using (SQLiteCommand cmd = new SQLiteCommand())                {                    cmd.Connection = con;                    cmd.CommandText = string.Format(@"select * from jy_dic_crop");//要顯示的資料                    int rows = cmd.ExecuteNonQuery();                    SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);                    DataSet ds = new DataSet();                    sda.Fill(ds);                    //con.Close();                    DataTable dtbl = ds.Tables[0];                    dataGridView1.AutoGenerateColumns = false;                    this.dataGridView1.DataSource = dtbl;            //綁定每列的值顯示在DatagridView                    this.dataGridView1.Columns["column1"].DataPropertyName = dtbl.Columns["id"].ToString();//column1是DatagridView的第一列的name值                    this.dataGridView1.Columns["column2"].DataPropertyName = dtbl.Columns["name"].ToString();                    this.dataGridView1.Columns["column3"].DataPropertyName = dtbl.Columns["status"].ToString();
            //將DatagridView的資料通過BindingSource與BindingNavigator串連起來 BindingSource bs
= new BindingSource(); bs.DataSource = dtbl; bindingNavigator1.BindingSource = bs; dataGridView1.DataSource = bs; } } } }}

 

效果如下:

DatagridView的資料與BindingNavigator導覽列聯絡起來了

 

 

BindingNavigator操作DatagridView的資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.