Update Table From DataGridView


  • I am using winforms and am querying sql server and returning the results to a datatable.  I am then setting the datatable as the datasource for a datagridview. 

    I am setting allowusertoaddrows & allowusertodeleterows = true - how can I have the additions and deletions update the database?

    I want the database update to take place on a button click - not on datagrid cell exit.  -> Now to explain this note a little further, I want the user to edit, delete, or add any rows in the grid.  Then press a button and have all edits/deletes/additions added to the database at one time
    • Moved by CoolDadTx Thursday, February 8, 2018 3:01 PM Winforms related
    • Edited by IndigoMontoya Thursday, February 8, 2018 3:30 PM
    Thursday, February 8, 2018 2:19 PM


  • Hi,

    You can use databinding with datagridview:

    Then code:

        public partial class FrmDgvBindingSource : Form
            public FrmDgvBindingSource()
            private void FrmDgvBindingSource_Load(object sender, EventArgs e)
                // TODO: This line of code loads data into the 'database100DataSet.Table1' table. You can move, or remove it, as needed.
            //delete current row
            private void button2_Click(object sender, EventArgs e)
            private void button3_Click(object sender, EventArgs e)
                DataRow dr = database100DataSet.Table1.NewRow();
                dr["Name"] = textBox1.Text;
                dr["Age"] = Convert.ToInt32(textBox2.Text);
                dr["Gender"] = textBox3.Text;
                database100DataSet.Table1.Rows.InsertAt(dr, table1BindingSource.Position);
                table1BindingSource.Sort = "Id ASC";
            private void button4_Click(object sender, EventArgs e)
                table1BindingSource.Sort = "Id DESC";
            private void button5_Click(object sender, EventArgs e)
                table1BindingSource.Filter = "Name='123'";



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    • Marked as answer by IndigoMontoya Friday, February 9, 2018 1:54 PM
    Friday, February 9, 2018 1:57 AM