locked
DELETE row and show previous row. RRS feed

  • Question

  • i have text boxes which  data from the dataset row.
    i have a row deletion code.
    what i want to do is after the row is deleted i want the text boxes data to be set to the data from the previous row.

    how do i do that ?

    the code is below :

    private void delete_Click(object sender, EventArgs e)
            {
                try
                {
                   DialogResult result=MessageBox.Show("Are you sure ?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                   if(result==DialogResult.OK)
                    {
                        DataRow[] dR = ds1.Tables[0].Select("book_id="+id.Text);
                        if (dR.Length > 0)
                        {
                            foreach (DataRow dRremove in dR)
                            {
                                ds1.Tables[0].Rows.Remove(dRremove);
                            }
                            ds1.AcceptChanges();
                        }
                        else
                            MessageBox.Show("Could not delete data.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (SqlException sqle)
                {
                    MessageBox.Show("Could not delete data.\n Error: "+ sqle.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                finally
                {
               
                }
            }
    }
    Saturday, September 19, 2009 4:57 PM

Answers

  • get the index of the row you are deleting, and after select row data which has one lower index.

    int currentrow=ds1.Tables[0].Rows.IndexOf(dR[0]);
    if(currentrow>0)
    {
    DataRow rowToshow=ds1.Tables[0].Rows[currentRow-1];
    .......
    }
    • Proposed as answer by Tamer Oz Saturday, September 19, 2009 5:37 PM
    • Marked as answer by Vital_Parsley705 Sunday, September 20, 2009 5:27 PM
    Saturday, September 19, 2009 5:07 PM

All replies

  • get the index of the row you are deleting, and after select row data which has one lower index.

    int currentrow=ds1.Tables[0].Rows.IndexOf(dR[0]);
    if(currentrow>0)
    {
    DataRow rowToshow=ds1.Tables[0].Rows[currentRow-1];
    .......
    }
    • Proposed as answer by Tamer Oz Saturday, September 19, 2009 5:37 PM
    • Marked as answer by Vital_Parsley705 Sunday, September 20, 2009 5:27 PM
    Saturday, September 19, 2009 5:07 PM
  • private void delete_Click(object sender, EventArgs e)
            {
                DataRow rowtoshow;
                int currentRow=0;
                try
                {
                   DialogResult result=MessageBox.Show("Are you sure ?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                   if(result==DialogResult.OK)
                    {
                        DataRow[] dR = ds1.Tables[0].Select("book_id="+id.Text);
                        if (dR.Length > 0)
                        {
                            foreach (DataRow dRremove in dR)
                            {
                                ds1.Tables[0].Rows.Remove(dRremove);
                            }
                            ds1.AcceptChanges();
                            currentRow = ds1.Tables[0].Rows.IndexOf(dR);
                            if(currentRow>0)
                                rowtoshow=ds1.Tables[0].Rows[currentRow-1];

                        }
                        else
                            MessageBox.Show("Could not delete data.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }


    this is the change io have made.
    But its showing error.

    Error1:    The best overloaded method match for 'System.Data.DataRowCollection.IndexOf(System.Data.DataRow)' has some invalid arguments    C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\sql_gui2\sql_gui2\Form1.cs    234    38    sql_gui2

    Error    2    Argument '1': cannot convert from 'System.Data.DataRow[]' to 'System.Data.DataRow'    C:\Documents and Settings\Mahesh\My Documents\Visual Studio 2005\Projects\sql_gui2\sql_gui2\Form1.cs    234    65    sql_gui2
    Saturday, September 19, 2009 5:25 PM
  • currentRow = ds1.Tables[0].Rows.IndexOf(dR);

    this line should be

    currentRow = ds1.Tables[0].Rows.IndexOf(dR[0]);

    because dR is an array
    Saturday, September 19, 2009 5:28 PM
  • rit! thanx for ur reply.

    Theres one more problem.

    When i click the exit button i want the data in the dataset to be sent to the database for this purpose i have the following code on my exit button.

    private void exit_Click(object sender, EventArgs e)
            {
                DialogResult result=MessageBox.Show("Do you want to make changes permanent ?.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    SqlCommandBuilder scd = new SqlCommandBuilder(da);
                    da.Update(ds1,"libdatatable");
                    Application.Exit();
                }
                else
                    Application.Exit();
            }

    But its not making the changes permanent in the database.Is the code rit ?
    Saturday, September 19, 2009 5:34 PM
  • Try removing

    ds1.AcceptChanges();

    from your code.
    Saturday, September 19, 2009 5:37 PM
  • if i remove
    ds1.AcceptChanges();
    from my code it wont update the dataset rit ?

    my idea is to let user make whatever changes he wants to make to the dataset and then when he closes application he is prompted if he want to make the changed permanent.

    So for that pupose i have the above code for my exit button.

    so how do i do that
    ?
    Saturday, September 19, 2009 5:41 PM
  • Accept changes changes all rows rowstate to Unchanged

    DataAdapter decides to execute insert,update or delete queries by the value of Rowstate.

    So if you call AcceptChanges none of the changes will be reflected to database.

    Also if you are using dataadapter and want to delete rows from database you should consider calling DataRow's delete methos instead of removing row from datatable.
    Saturday, September 19, 2009 5:52 PM
  • Thanx for all the help provided Tamer Oz .
    But the above delete code is noit setting the previous row as the current row.

    DataRow[] dR = ds1.Tables[0].Select("book_id="+id.Text);
                        if (dR.Length > 0)
                        {
                            foreach (DataRow dRremove in dR)
                            {
                                ds1.Tables[0].Rows.Remove(dRremove);       //This only returns one row as book_id is a primary key(not autoincrementing).
                            }
                            ds1.AcceptChanges();
                            currentRow = ds1.Tables[0].Rows.IndexOf(dR[0]); //this i think returns -1
                            if(currentRow>0)            //so this if block does not execute.
                            {
                                rowtoshow=ds1.Tables[0].Rows[currentRow-1];
                                fillDataSetTable();
                            }
                        }

    So how do i set the previous block as current row!!! help!!
    the currentrow gets the value -1.....when doe this happen ?
    help!!!
    Sunday, September 20, 2009 4:28 AM
  • You can not find index of dR[0] after removing it from rows collection of table

    Move the code before the line you remove row from table, also as I mentioned previous post consider using delete method of row instead of removing row.
    Sunday, September 20, 2009 4:43 AM
  • thanx for ur immediate replies...but theres just 1 last problem.

    if i try to delete the last to row from the dataset it does not show the previous row..instead it throws an error.!

    why is it so ?
    Sunday, September 20, 2009 5:28 PM
  • No there should not be a problem if you decrase the index by one.

    Problem you have mentioned could be occur when first row is deleted.

    if(currentRow>0)  // this code is for that situation.

    You can change it like below

    if(currentRow>0)            //so this if block does not execute.
    {
    rowtoshow=ds1.Tables[0].Rows[currentRow-1];
    fillDataSetTable();
    }
    else

    {
    towtoshow=ds1.Tables[0].Rows[0];
    fillDataSetTable();
    }
    Sunday, September 20, 2009 5:56 PM
  • THANK YOU FOR ALL YOUR REPLIES!
    it was very helpful.
    Monday, September 21, 2009 5:38 AM