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导航栏联系起来了
时间: 2024-10-29 19:08:34