Hi Guys,

I have a form with a DGV and some text boxes.. I am trying to get the program to update the rows shown on the DGV by using the values in the text boxes.

Below is my code so far:

       private void btnOrderEdit_Click(object sender, EventArgs e)
        {

            System.Data.OleDb.OleDbCommandBuilder cb;
            cb = new System.Data.OleDb.OleDbCommandBuilder(cashDA);

            System.Data.DataRow dRow2 = cashOrdersDS.Tables["cashOrders"].Rows[0];

            dRow2[1] = Int64.Parse(txtOrderQTY.Text);
            dRow2[2] = txtBoxDescrip.Text.ToString();
            dRow2[3] = txtBoxSupplier.Text.ToString();
            dRow2[4] = dateTimePicker1.Value.ToString();
            dRow2[5] = Convert.ToDouble(txtBoxCost.Text);
            dRow2[6] = Convert.ToDouble(txtBoxSell.Text);
            dRow2[7] = Int64.Parse(textBox1.Text);

            cashDA.Update(cashOrdersDS, "cashOrders");

            MessageBox.Show("Data Updated");

        }

Any help would be great!

I'm guessing if I can resolve this one I can resolve the 'Delete Row' Problem as well.

      private void btnOrderDelete_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbCommandBuilder cb;
            cb = new System.Data.OleDb.OleDbCommandBuilder(cashDA);

            cashOrdersDS.Tables["cashOrders"].Rows[   ].Delete();

            cashDA.Update(cashOrdersDS, "cashOrders");

            MessageBox.Show("Record Deleted");
        }

Looking into this a bit further I think I have to do the following:

       private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int i, j;

            i = dataGridView2.CurrentCell.RowIndex;
            j = dataGridView2.CurrentCell.RowIndex;





       private void btnOrderEdit_Click(object sender, EventArgs e)
        {


            System.Data.OleDb.OleDbCommandBuilder cb;
            cb = new System.Data.OleDb.OleDbCommandBuilder(cashDA);

            System.Data.DataRow dRow2 = cashOrdersDS.Tables["cashOrders"].Rows[i][j];

            dRow2[1] = Int64.Parse(txtOrderQTY.Text);
            dRow2[2] = txtBoxDescrip.Text.ToString();
            dRow2[3] = txtBoxSupplier.Text.ToString();
            dRow2[4] = dateTimePicker1.Value.ToString();
            dRow2[5] = Convert.ToDouble(txtBoxCost.Text);
            dRow2[6] = Convert.ToDouble(txtBoxSell.Text);
            dRow2[7] = Int64.Parse(textBox1.Text);

            cashDA.Update(cashOrdersDS, "cashOrders");

            MessageBox.Show("Data Updated");

        }

If anyone could help me out here that would be great!

The dataadapter wont update the database without you opening a connection to it prior to the update call and closing it after.

If the datagrid isnt updating make a little method that unbinds its datasource, and then rebinds it again, then call that after you update the row. Thats how i've done it before anyway :)

Edited 4 Years Ago by Mike Askew

Good morning Mikey,

That does'nt seem to work..

Code used:

       private void unbind()
    {
        dataGridView2.DataSource = null;
        dataGridView2.DataSource = cashOrdersTable;
    }

        private void btnOrderDelete_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbCommandBuilder cb;
            cb = new System.Data.OleDb.OleDbCommandBuilder(cashDA);

            if (dataGridView2.SelectedRows.Count > 0)
            {
                cashOrdersTable.Rows.RemoveAt(dataGridView2.SelectedRows[0].Index);



                cashDA.Update(cashOrdersDS.Tables["cashOrders"]);

                unbind();

                MessageBox.Show("Record Deleted");
            }
            else
            {
                MessageBox.Show("Please Select a row");
            }
        }

Hmm sec let me dig back through some old code, swear there was a third line in that code block.

Updated:

I used to use:

.SuspendLayout();
.DataSource = null;
.DataSource = AB.Persons; //AB.Persons being a table in a dataset
.ResumeLayout();

However thats no different from what your running really apart from the layout suspension which means the unbind and rebind is not noticeable as the datagrid wont redraw until the resume is called.

OHHHHHHH I SEE IT.

Your making dRow equal to the row in the table. and then doing nothing with it, it is a seperate object to the table and so it wont update that row when you change properties of it. Change all the dRow calls to the row cell identities instead and it would work, else you can (i think) set the row to equal the dRow after filling it? One of those should work :)

Edited 4 Years Ago by Mike Askew

HI Mikey,

Thanks for your reply. I am guessing by 'Indentities' you mean the names of the Row's I'm calling?

If a invidiual Cell has a ID how to I get the system to pick that up and pass it over?

I meant as in where you assign dRow, cashOrdersDS.Tables["cashOrders"].Rows[i][j]; use this to individually map each field to a textbox.

for example cashOrdersDS.Tables["cashOrders"].Rows[i][j] = Textbox1.Text; something along those lines

This article has been dead for over six months. Start a new discussion instead.