I have a GridView control hooked up to a datasource that I allow row editing on. I use the standard edit/save/delete buttons that point to the Update, Cancel, and edit GridView commands.
I would like to add an additional button, "Next" inline with the save/cancel button. This would function the same as the save button, but would also add an additional row to the grid, and place that row into edit mode. To do this, I've added code to the Updated
event of the GridView, which adds a new record to the db, rebinds the grid, and sets the editindex. This seems work, but the grid does not actually return in edit mode. I stripped out the new record code and simply left the statement to set the editindex, but the grid still will not stay in edit mode.
Is it possible to force the grid to stay in edit mode in this scenario? It appears that unless you set the cancel property of the GridViewUpdatedEventArgs
, the Grid is going to flip to view mode. I don't want to set the cancel property in this case, because I want the GridView to trigger my datasource object to save the record.
If this doesn't work, it appears that I will need to allow the update to occur normally, return to the client, and then send another server request to add the record. I'd prefer not to do it this way, I would like to perform the necessary operations in context of one roundtrip.
Here's what the code (simplified) looks lik开发者_如何学Goe:
protected void Button1_Click(object sender, EventArgs e)
{
// Works Fine
this.sampleDataSource.Insert();
this.sampleGridView.DataBind();
this.sampleGridView.EditIndex = this.sampleGridView.Rows.Count - 1;
}
protected void sampleGridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
// Grid will display new row, but will not be in edit mode
this.sampleDataSource.Insert();
this.sampleGridView.DataBind();
this.sampleGridView.EditIndex = this.sampleGridView.Rows.Count - 1;
}
I've found a solution that appears to do what I want. Basically I need to let the GridView go through the normal event sequence, then set the editindex. Here's the code, again, simplified:
protected void sampleGridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
this.insertNewRow = true;
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
if (this.insertNewRow)
{
this.sampleDataSource.Insert();
this.sampleGridView.DataBind();
this.sampleGridView.EditIndex = this.sampleGridView.Rows.Count - 1;
}
}
We'll probably need to see some code... But, I can guess where the problem is from your description. You probably need to set EditIndex prior to rebinding. If that doesn't work (out of range exception?), then you'll have to call DataBind() to get your new row, set EditIndex, call DataBind() again.
Unfortunately, just setting the EditIndex doesn't really get the job done. You have to set the index AND call DataBind().
I had similar problem with setting up needed index. This solution worked for me: grdProjectsForSubmiting - GridView for displaying data
grdProjectsForSubmiting.EditIndex = neededIndex; // not forget, that rows enumerates from 0, not from 1 !!
grdProjectsForSubmiting.DataSource = ProjectsController.SessionTodayEntries;
grdProjectsForSubmiting.DataBind();
精彩评论