Gridview mode not changing back to Readonly after Update

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stefbek97
    New Member
    • Aug 2009
    • 54

    Gridview mode not changing back to Readonly after Update

    Hello All,

    I am having Trouble with a Gridview control In my asp.net/C# application. It contains Some boundFields and one checkbox field. My gridview allows Editing. It succesfully enters edit mode and when i hit update the Datasource updates but the control will not return to readonly mode. I have gone far enough to figure out the problem is caused by the Checkbox field.The checkbox works and passes the correct true/false value to the datasource but will not bind after update When I Make it readonly and remove that value from my Stored Procedure the Gridview control works normal. Please help.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I don't understand what you're doing.

    You have a GridView that has a checkbox field.
    You Edit a record in the Grid view and update the database.

    Now what what are you doing??

    What is the problem?

    Comment

    • stefbek97
      New Member
      • Aug 2009
      • 54

      #3
      Not Changing Mode

      Usually when you have a Gridview than can be edited you click the edit button and all the editable fields become Textboxes and the Checkbox is Enabled. I change the values and click update but the gridview stays in Edit mode instead of canging back to Readonly and displaying the new results. If i hit cancel the Grid changes modes and i see the new values. Usually the gridview returns to readonly after update's. I figured out this problem is caused by the Checkbox field . Once i remove this field from my gridview it works (changes modes back and forth) Is there an alternative to the Checkbox that will return true/false?

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Once you're finished updating have you tried setting the GridView's EditIndex property to -1?

        Like (vb):
        Code:
         Private Sub myGridView_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles myGridView.RowUpdated
                myGridView.DataSource = _source
                myGridView.EditIndex = -1
                myGridView.DataBind()
        End Sub
        (C#)
        Code:
         Private void myGridView_RowUpdated(Object sender,  System.Web.UI.WebControls.GridViewUpdatedEventArgs e) 
        {
                myGridView.DataSource = _source;
                myGridView.EditIndex = -1;
                myGridView.DataBind();
        }

        Comment

        • stefbek97
          New Member
          • Aug 2009
          • 54

          #5
          Grridview Not updating

          Yes I have tried that. Sorry I should have posted. That does not resolve the problem.
          Thanks

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Please post the ASP code for your GridView and post your C# code for the GridView's Update method.

            Comment

            • stefbek97
              New Member
              • Aug 2009
              • 54

              #7
              Gridview Mode not Changing

              Code:
              <asp:GridView ID="gvModules" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSourceModulesManage"
               EmptyDataText="No Modules" SkinID="GridViewSmall" Visible="False" DataKeyNames="ModuleId" onrowupdated="gvModules_RowUpdated">
                                  <Columns>
                                      <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" ButtonType="Button" />
                                      <asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number" />
                                      <asp:BoundField DataField="MODName" HeaderText="MODName" ReadOnly="True" SortExpression="MODName" />
                                      <asp:BoundField DataField="ModuleSubType" HeaderText="ModuleSubType" ReadOnly="True"
                                          SortExpression="ModuleSubType" />
                                      <asp:BoundField DataField="ModelName" HeaderText="ModelName" ReadOnly="True" SortExpression="ModelName" />
                                      <asp:BoundField DataField="TargetParticipantUseDays" HeaderText="TargetParticipantUseDays"
                                          SortExpression="TargetParticipantUseDays" />
                                      <asp:CheckBoxField DataField="NeedsParticipantCompliance" HeaderText="NeedsParticipantCompliance"
                                           SortExpression="NeedsParticipantCompliance" />
                                      <asp:BoundField DataField="AcceptableWeekDays" HeaderText="AcceptableWeekDays" 
                                          SortExpression="AcceptableWeekDays" />
                                      <asp:BoundField DataField="AcceptableWeekendDays" HeaderText="AcceptableWeekendDays"
                                          SortExpression="AcceptableWeekendDays" />
                                      <asp:BoundField DataField="AcceptableHours" HeaderText="AcceptableHours"
                                          SortExpression="AcceptableHours" />
                                  </Columns>
               </asp:GridView>
              I am using SqlDataSource and do not have C# code for update. The onrowupdated code is exactly like to code you posted earlier.
              Last edited by Frinavale; Aug 19 '09, 06:40 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                ...then where are you setting the GridView's EditIndex = -1?

                Comment

                • stefbek97
                  New Member
                  • Aug 2009
                  • 54

                  #9
                  I set EditIndex to -1 in the Properties window in design mode.

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Try implementing a method that handles the GridView's RowUpdated event.
                    In that method set the EditIndex to -1.

                    Comment

                    • stefbek97
                      New Member
                      • Aug 2009
                      • 54

                      #11
                      As i mentioned earlier i am using the OnRowUpdated Code like you posted earlier in the OnRowUpdated Event Handler and settint the EditIndex to -1 and Calling the Gridview.DataBi nd() Method.

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        Please post that code then :)

                        Comment

                        • stefbek97
                          New Member
                          • Aug 2009
                          • 54

                          #13
                          Code:
                          protected void gvModules_RowUpdated(object sender, GridViewUpdatedEventArgs e)
                              {
                                  gvModules.DataSourceID = "SqlDataSourceModulesManage";
                                  gvModules.EditIndex = -1;
                                  gvModules.DataBind();
                          
                              }

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            Hi stefbek97,

                            Something in your question is not adding up.

                            I took your GridView, created an ObjectDataSourc e that matched the Fields that the GridView was displaying and bound the GridView to it so that I can see what you're "seeing".

                            What I discovered is that you only have a Select and a Delete Button.

                            There is no control in the GridView that you posted used for "Updating".

                            So how can you be having a problem with the GridView Mode change after an "Update" occurs.

                            Your question doesn't make any sense.

                            Comment

                            • stefbek97
                              New Member
                              • Aug 2009
                              • 54

                              #15
                              Hello Frinavale,

                              I understand your concern. I tried to delete and create the gridview over and over to see if there are any errors I made. I changed it from Template fields to boundFields. From CommandField to Buttons "Edit","Update" ,"Delete" with matching Command Name's. My fault i submitted the code and did not verify it. If you and the ShowEditButton= "true" please let me know if you can see what i am seeing.

                              Comment

                              Working...