0

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.

Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17
RSullivan
  • 65
  • 7
  • You can have a look at my answer to the following post: http://stackoverflow.com/questions/36827111/asp-net-gridview-how-to-edit-and-delete-data-records/36828018#36828018. You will see that, in `OnRowEdit`, you must first set the `EditIndex` and then rebind the data (at least calling `GridViewPackaging.DataBind()`). – ConnorsFan May 06 '16 at 00:37
  • Sorry but I am having problems editing a comment. – RSullivan May 06 '16 at 03:25
  • Sorry. I have never used StackOverflow before. Though it does not make sense to me I set .EditIndex before GridViewPackagingSelectStatement() in OnRowEdit. I added .DataBind() after .SelectCommand= in GridViewPackagingSelectStatement(). (It binds twice now as I suspected). Nothing has changed, Units still has no length when I am in edit mode. – RSullivan May 06 '16 at 03:39

0 Answers0