I have a dgv which I bind to a datatable.
public Form1()
{
da = new SqlDataAdapter("Select * from workers order by id", connStr);
cb = new SqlCommandBuilder(da);
dt = new DataTable();
}
Here I fill the datatable and link the gridview to the datasource
private void button1_Click(object sender, EventArgs e)
{
dt.Clear();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
When I want to add a new row to the datagridview I do the following:
private void button2_Click(object sender, EventArgs e)
{
dt.Rows.Add(dt.NewRow());
da.Update(dt); //reflects changes to database
dt.Clear();
da.Fill(dt);
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count - 1;
}
Is there a way to add the row without having to reload the whole table?