namespace UpdateDataWindowsFormDataGridView
{
public partial class Form1 : Form
private SqlDataAdapter da;
private DataTable dt;
public Form1()
InitializeComponent();
// Configure display characteristics of the data grid view
dataGridView.Anchor = AnchorStyles.Left | AnchorStyles.Right |
AnchorStyles.Top | AnchorStyles.Bottom;
this.Width = 500;
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
// Set the data source of the data grid view
dataGridView.DataSource = LoadData();
private DataTable LoadData()
string sqlConnectString = @"my connetion";
string sqlSelect = "SELECT Id, IntField, StringField FROM DataGridView";
// Create a data adapter and command builder
da = new SqlDataAdapter(sqlSelect, sqlConnectString);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
// Load a DataTable with schema and data from table DataGridView
dt = new DataTable();
da.FillSchema(dt, SchemaType.Source);
da.Fill(dt);
return dt;
private void saveButton_Click(object sender, EventArgs e)
da.Update(dt);
MessageBox.Show("Changes saved.", "DataGridView",
MessageBoxButtons.OK, MessageBoxIcon.Information);
This is not my code but I just did exactly like this but on a extra note I have added a button which selects the value in the gridview…nw I want a button for just updating the gridview… wen I use this code the database remains unchanged(nothing happens)…. For the second button I have added the SQL dataadaptor and datatable publically hw t is in the prog but with a different object instance(name)..
This code is workin if I create a dummy form with just one gridview and a button, but not wen 2 buttons (one for select and 1 for update ).. is there any other way to do this..