Binding a Datagridview and binding navigator with code
Here is a c# example on how to bind a datagridview and bindingnavigator with code. I manually added the save button to the bindingnavigator.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CSharpBindingSource
{
public partial class Form1 : Form
{
SqlDataAdapter da;
SqlConnection conn;
DataSet ds;
BindingSource bs;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
String strConn = "Server = .;Database = NorthWind;Integrated Security = SSPI;";
conn = new SqlConnection(strConn);
da = new SqlDataAdapter("Select * From Employees", conn);
SqlCommandBuilder cmd = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "Employees");
bs = new BindingSource(ds, "Employees");
dataGridView1.DataSource = bs;
bindingNavigator1.BindingSource = bs;
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
bs.EndEdit();
da.Update(ds, "Employees");
}
}
}