1

I have a gridview I want that when I click on "edit" button in gridview then edit row values inserted in the controls which is outside the gridview and then row is deleted. Can I call "delete" event method inside the "edit" event, because I want to first retrieve values in controls and then row is deleted.

Here is my aspx.cs code. Both delete and edit event code here :

protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    DataTable t = (DataTable)Session["MyDataTable"];
    t.Rows.RemoveAt(e.RowIndex);
    GridView2.DataSource = t;
    GridView2.DataBind();
}

public void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
    DataTable t = (DataTable)Session["MyDataTable"];
    TextBox1.Text=GridView2.Rows[e.NewEditIndex].Cells[1].Text.ToString();

    DropDownList1.Text = GridView2.Rows[e.NewEditIndex].Cells[2].Text.ToString();
    GridView2.EditIndex = -1;
    GridView2.DataSource = t;
    GridView2.DataBind();

    //CAN I CALL GRIDVIEW_ROW_DELETING method here? I try but problem is arguments etc
}

2 Answers 2

2

As Emanuaele said, there's better ways to do this, but if you want to keep your code close to what you have, move your delete code into a separate method

protected void DeletingRow(int rowIndex)
{
    DataTable t = (DataTable)Session["MyDataTable"];
    t.Rows.RemoveAt(rowIndex);
    GridView2.DataSource = t;
    GridView2.DataBind();
}

Then GridView2_RowDeleting would change to

protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    DeletingRow(e.RowIndex);
}

And GridView2_RowEditing would change to

public void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
    DaTable t = (DataTable)Session["MyDataTable"];
    TextBox1.Text=GridView2.Rows[e.NewEditIndex].Cells[1].Text.ToString();
    DropDownList1.Text =   GridView2.Rows[e.NewEditIndex].Cells[2].Text.ToString();
    GridView2.EditIndex = -1;
    GridView2.DataSource = t;
    GridView2.DataBind();
    //CAN I CALL HERE GRIDVIEW_ROW_DELETING METHOD,I try but problem is arguemnts etc

    DeletingRow(e.NewEditIndex);
}
Sign up to request clarification or add additional context in comments.

9 Comments

@March thanks for answer,but there is one red line error come.in "Gridview2_RowEditing" error come at last line DeletingRow(GridView2.RowIndex); //at RowIndex, because method contain GridViewEditEventArgs e pass
does not contain defination of RowIndex
Sorry should be CurrentRow.Index
I check it now,but it's not working,here is snap shot hostthenpost.org/uploads/2343991ab460f29ec78fea625b680e43.png
Maybe it's e.NewEditIndex But it's not correct! It's better to use RowCommmand
|
0

Approach is wrong if data comes from a database. You have to work on data, not on controls of page (you can but it's better not to do).

Don't use Session to store Datatable (how many records are there in?)

Use CommandArgument of your button to get the Id of the record. Retrieve record from database and store fields in controls. Click a button, save data, delete the record and rebind.

Using your approach, you can move the code of deleting event in a private method and call it from editing event

2 Comments

I don't want any database connectivity right now..Actaully my idea is first user insert values into gridview and then press button "Create table" then all values inserted into the database..First tell me pelase,can i call delete event method in edit event?
Read Marc's answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.