GridView Editing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • movieking81
    New Member
    • Feb 2007
    • 52

    GridView Editing

    Ok, so since my last post I've tested the Gridview and DataView controls and it appears as though those would work a little better. However, I'm having difficultly implementing the "Edit mode" on those controls to allow the user to edit the data. I can't really seem to find a "step by step" example of of how to do it. Any assistance would be great.

    Thank again
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    What exactly are you having problems with?
    If you want to edit a row then implement a method that handles the GridView.RowEdi ting event and set the GridView.EditIn dex to the e.NewEditIndex to edit the row that was selected for editing.

    For example:

    Code:
            protected void TableGridView_RowEditing(Object sender, GridViewEditEventArgs e)
            {
                TableGridView.EditIndex = e.NewEditIndex;
            }
            protected void TableGridView_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
            {
                TableGridView.EditIndex = -1;
            }
    Please post new questions in their own thread.
    I have split this question off of your other thread on TextBox Loop C#

    -Frinny

    Comment

    • movieking81
      New Member
      • Feb 2007
      • 52

      #3
      I need to write the matrix to the screen and let the user update whichever cell they need (i.e. textboxes). Once they are complete they would click a submit button to update the database. An example is below.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Hmm so you need every cell to be editable.

        In this case I think that you are going to want to use a GridView for editing purposes (where every element is displayed in TextBoxes) and a GridView for display purposes...simp ly because I'm not sure how you would make every row editable at the same time in edit mode.

        Do you see what I'm getting at?

        -Frinny

        Comment

        • movieking81
          New Member
          • Feb 2007
          • 52

          #5
          Ok, so I would add the textboxes to the gridview like so right?

          Code:
              <asp:Gridview id="ItemsGrid" runat="server" BorderColor="black" BorderWidth="1" CellPadding="3" AutoGenerateEditButton="False" AutoGenerateColumns="False">
               
               <Columns>  
          <asp:TemplateField HeaderText="R1C1" SortExpression="r1c1">  
          <EditItemTemplate>  
          <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("r1c1") %>'>
          </asp:TextBox>  
          </EditItemTemplate>  
          
          <ItemTemplate>  
          <asp:Label ID="Label1" runat="server" Text='<%# Bind("r1c1") %>'>
          </asp:Label>  
          </ItemTemplate> 
          </asp:TemplateField> 
               </Columns> 
              
              </asp:Gridview>
          Because all I get is a readonly display of the data. I don't see the textboxes anywhere. Thanks again for all the help.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            That's because you put the TextBox in the <EditItemTempla te> section of your TemplateField.

            Move the TextBox to the <ItemTemplate > section instead (you don't have one right now).

            :)

            -Frinny

            Comment

            • movieking81
              New Member
              • Feb 2007
              • 52

              #7
              Great, I got it working with the following...

              Code:
                   <Columns>  
              <asp:TemplateField>   
              
              <ItemTemplate>
              <asp:TextBox ID="TextBox1" runat="server" size="2" Text='<%# Bind("r1c1") %>'>
              </asp:TextBox> 
              <asp:TextBox ID="TextBox2" runat="server" size="2" Text='<%# Bind("r1c2") %>'>
              </asp:TextBox> 
              <asp:TextBox ID="TextBox3" runat="server" size="2" Text='<%# Bind("r1c3") %>'>
              </asp:TextBox> 
              <asp:TextBox ID="TextBox4" runat="server" size="2" Text='<%# Bind("r1c4") %>'>
              </asp:TextBox>     
              </ItemTemplate>
              
              </asp:TemplateField> 
                   </Columns>
              However, I have 102 fields in the SQL table. Do I have to insert 102 textbox fields? Is there any easier way? Is there a If or while I can execute?

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Oh...102 is quite a bit.

                You could consider doing this dynamically I guess but it's not that straight forward. I had to do something similar before...I had 4*255, or 8*255, or 16*255 items that I needed to display as links. I decided to create a TemplateItem with links in it dynamically but it was not as straightforward as I thought it was going to be....

                Mind you with TextBoxes you don't have to worry about events being thrown by the TextBox so it might be a bit easier.


                Doing this by hand will be a lot easier to implement than doing it dynamically...b ut you can do this dynamically if you choose to.

                -Frinny

                Comment

                • movieking81
                  New Member
                  • Feb 2007
                  • 52

                  #9
                  Great, can you give me a starting point for creating the textboxes dynamically? How would I start that?

                  Thanks

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Well, like I said this is not exactly the easiest thing to do (the first time anyways) because there are a lot of things that you have to keep in mind when you implement this.

                    The first thing I suggest you do is read over this quick overview on how to use dynamic controls in ASP.NET.

                    The important thing to take from this is that if you want to be able to retrieve Text from your dynamic TextBoxes...or if you want to be able to handle an Event that some control raised...you need to instantiate the dynamic controls in the Page_Init event.

                    In your case you are going to be dynamically adding TemplateFields ("columns") to your GridView in the Page_Init event....so you need to know how many columns you need to add.

                    The Page_Init event occurs very early in the life cycle. You don't have a lot of things available to you at that time so be aware that things like "IsPostback " won't work properly. This is because, things like "IsPostback " are initialized after the ViewState for the page is loaded, and the Page_Init event occurs just before the ViewState is loaded. You can still use things like Session or access cookies or use the query string at this point though. The whole reason why it's important that your instantiate your dynamic controls in the Page_Init is so that the objects exist when the ViewState is loaded...in other words if your TextBoxes don't exist when the ViewState is loaded for them, then your TextBoxes will not contain any Text (which may be loaded from the ViewState).

                    Ok, now that that very important point has been covered, I recommend that you look over the ITemplate Interface. This interface lets you define a class that will you can use as the TemplateField. You will have to create a custom "TemplateFi eld" class and in there add your TextBoxes to the class.

                    You should probably also look at the INamingContaine r Interface so that you get a good idea of what's going on in general (you should probably read this first actually).

                    Both of these topics might take you a while to go through.
                    Like I said this is not exactly easy to do the first time round because there is a lot of new topics and concepts to grasp before you will get a working solution.

                    -Frinny

                    Comment

                    • movieking81
                      New Member
                      • Feb 2007
                      • 52

                      #11
                      I'm grateful for all the help. It's going to take me a while to go through all this.

                      Thanks Again

                      Comment

                      Working...