I have a GridView.
It has many fields on it.
There is only one field in a row that I want to be able to edit called 'Units'.
'Units' is the only field with ReadOnly="true"
(all the rest are ReadOnly="false"
);
I am also using an OnRowDataBound
In the OnRowDataBound
I perform some calculations that involve the value
of 'Unit' to populate some templatefield / itemtemplate values (Unbound Columns).
My problem is when I click the 'Edit' button on a row:
1.) OnRowEdit()
is called which re-creates the SelectCommand
if required.
If I do not do this the contents of the GridViewPackaging disappears.
2.) The OnRowdataBound()
will be called due to the GridViewPackaging.DataBind()
.
3.) In the OnRowDataBound()
the value for 'Units' (Cells[8].Text
) is an empty
string.
Why is this so?
If I set 'Units' to ReadOnly="false"
then the value for Units (Cells[8].text
)
is now there within the OnRowdataBind()
.
private void GridViewPackagingSelectStatement()
{System.String SelectStatment;
// Giant SQL Multiple table INNER JOIN, WHERE removed for readability.
this.SqlDataSourcePackaging.SelectCommand=SelectStatment;
}
protected void GridViewPackaging_OnRowEdit(object sender,GridViewEditEventArgs e)
{
// If I Do Not Do This Then My GridView Will Not be Populated (i.e. it Will Be Empty).
this.GridViewPackagingSelectStatement();
this.GridViewPackaging.EditIndex=e.NewEditIndex;
}
protected void GridViewPackaging_UnboundColumns(object sender,GridViewRowEventArgs e)
{System.Int32 UsageIndex=0;
System.Int32 CurrentUnits=0;
System.Data.SqlTypes.SqlMoney CostPerUnit=0;
if(e.Row.RowType==DataControlRowType.DataRow)
{
// Removed Code For readability
// e.Row.Cells[8] Is 'Units'
// This Code Structure Is For Debugging
if((e.Row.RowState&DataControlRowState.Edit)!=DataControlRowState.Edit)
{
if(e.Row.RowIndex==1) // For Debugging, For Breakpoint On Next Line
{
// Cells[8] Always Has The Correct Value When I Am Not Editing the Row
CurrentUnits=Convert.ToInt32(e.Row.Cells[8].Text);
}
CurrentUnits=Convert.ToInt32(e.Row.Cells[8].Text);
}
else
{
if(e.Row.RowIndex==1) //For Debugging, For Breakpoint On Next Line
{
// When Editing Length Is Always 0, .Text=""! Why Is This So?
// If I Set Unit (Which Is Row[8]) To ReadOnly="false" Then The
// Value Is There But I Will Not be Able To Edit It. Huh?
if(e.Row.Cells[8].Text.Length!=0)
{
CurrentUnits=Convert.ToInt32(e.Row.Cells[8].Text);
}
}
}
// Removed Code For readability
}
Thanks.