First of all, many thanks to Steve Robins for getting me started down the right track. My approach was slightly different, partial due to the fact I was not using a DataBind method.
I use a combination of the OnRowDataBound and OnDataBound events of the DataView. The OnRowDataBound event to determine the index of the row to be put into edit mode. The OnDataBound event sets the index and rebinds the DataView.
A variable is used to ensure that the View is not continuously rebound.
Here is the crux of the aspx page edited down for brevity.
<asp:GridView ID="GridView1" runat="server"
DataSourceID="CreativeTypeDS"
AutoGenerateColumns="False"
DataKeyNames="SourceCreativeTypeID"
EmptyDataText="No Cretive Types at this time."
CellPadding="3" CellSpacing="1"
OnRowDataBound="GridView1_RowDataBound"
OnDataBound="GridView1_DataBound" >
<Columns>
[Template Columns Here]
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="CreativeTypeDS" runat="server"
SelectCommand="cmsSourceCreativeTypeSel"
SelectCommandType="StoredProcedure"
UpdateCommand="cmsSourceCreativeTypeUpd"
UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="Active" DefaultValue="0" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="SourceCreativeTypeID" Type="Int32" />
<asp:Parameter Name="SourceCategoryID" Type="Int32"/>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Active" Type="Boolean" />
</UpdateParameters>
</asp:SqlDataSource>
And now for the code behind.
public partial class SourceCreativeTypes : System.Web.UI.Page
{
private int? EditIndex = null;
private bool GridRebound = false;
protected override void OnPreInit(EventArgs e)
{
CreativeTypeDS.ConnectionString = "[CONNECTION STRING MAGIC HERE]";
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
//Dont Want to set edit row manualy if post back
GridRebound = true;
}
}
//Use the Row Databound Event to find the row to put into edit mode
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (!String.IsNullOrEmpty(Request.QueryString["ID"]))
{
//Get Target ID from Query String
string sSourceCreativeTypeID = Request.QueryString["ID"];
//Get data for row being bound
DataRowView rowView = (DataRowView)e.Row.DataItem;
// Set index if we are in a "normal row", the row data
// has been retrieved
// and the Supplied ID matches that of this row
if ((e.Row.RowState == DataControlRowState.Normal ||
e.Row.RowState == DataControlRowState.Alternate)
&&
(rowView != null &&
rowView["SourceCreativeTypeID"].ToString() == sSourceCreativeTypeID)
)
{
EditIndex = e.Row.RowIndex;
}
}
}
/* Use the Datbound Event to set the required row to index mode
* Then Rebind the grid. Rebinding in Row Databound does not work
* for Reasons unknown */
protected void GridView1_DataBound(object sender, EventArgs e)
{
//Set Gridview edit index if one is supplied and page is not a post back
if (!GridRebound && EditIndex != null)
{
//Setting GridRebound ensures this only happens once
GridRebound = true;
GridView1.EditIndex = (int)EditIndex;
GridView1.DataBind();
}
}
}
}
Hopefully between Steve and Myself we help a few of you out there