none
Update Table From DataGridView

    Question

  • 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 CoolDadTxMVP Thursday, February 08, 2018 3:01 PM Winforms related
    • Edited by IndigoMontoya Thursday, February 08, 2018 3:30 PM
    Thursday, February 08, 2018 2:19 PM

Answers

  • Hi,

    You can use databinding with datagridview:

    Then code:

        public partial class FrmDgvBindingSource : Form
        {
            public FrmDgvBindingSource()
            {
                InitializeComponent();
            }
    
            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.
                this.table1TableAdapter.Fill(this.database100DataSet.Table1);
            }
    
            //delete current row
            private void button2_Click(object sender, EventArgs e)
            {
                table1BindingSource.RemoveCurrent();
                table1BindingSource.EndEdit();
                table1TableAdapter.Update(database100DataSet.Table1);
            }
    
            //insert
            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.EndEdit();
                table1TableAdapter.Update(database100DataSet.Table1);
                table1BindingSource.Sort = "Id ASC";
            }
    
            //sort
            private void button4_Click(object sender, EventArgs e)
            {
                table1BindingSource.Sort = "Id DESC";
            }
    
            //Filter
            private void button5_Click(object sender, EventArgs e)
            {
                table1BindingSource.Filter = "Name='123'";
            }
        }

    Regards,

    Stanly


    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 MSDNFSF@microsoft.com.

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