Retrieve data from dynamic gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agamoto
    New Member
    • Dec 2008
    • 2

    Retrieve data from dynamic gridview

    hello guys,

    i don't know if this is the right tread to post but i have the following problem:

    i've built a dynamic gridview in run time because the gried values (columns, headers, etc) are conditioned with values that come from the database. I used TemplateField's to do that. i don't have a active connection to the database. i store my information to a dataset and bind it to the gridview

    another thing is that all rows in m gridview are in editable mode.

    now, i'm trying to retrieve the data from the grid to store at database and doesn't work.

    if i try something like this
    string value = ((TextBox)row.F indControl("NAM E")).Text;
    only works for a static control. For a dynamic control it gives a null exception.

    i really feel that i'm going a wrong way. this should be more simple.

    any advise?

    tks in advance
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I am confused. GridView by design is dynamic. Populate with a data source of some sort and it fills itself in with columns/rows and such.

    Then you can use the column name and a row index to pick out information.

    Comment

    • agamoto
      New Member
      • Dec 2008
      • 2

      #3
      the problem was that i don't know how many columns my grid will have, because the columns depends of another table. at run time i build the dataset and then bind it to the grid. Because of that, i can't refer explicitly the names or index the columns :|

      But that's ok. I moved to another solutions.

      tks

      Comment

      • tomasjons
        New Member
        • Apr 2009
        • 7

        #4
        Hi agamoto

        Witch solution did you move to? I am having the same problem.

        Tomas

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          I'm doing something similar.

          I'm dynamically creating the template fields depending on what I retrieve from the database and add them to my GridView.

          This is not easy to do.

          You're going to have to create the TemplateFields during the Page Init event so that the GridView can be loaded with the data entered by the user.

          I store the number of columns in a hidden field so that I can use the Request.Params( ) method to retrieve the number of columns for the GridView in the PageIinit event. If this number is greater than 0, then I dynamically create the TemplateFields and add them to the GridView.

          After the Page Init event happens the TemplateFields are loaded with data from ViewState. This will load the GridView in "edit mode" and load the data entered by the user into the TemplateFields so that you can access them.

          Please see this quick article on how to use dynamic controls in asp.net for more information.

          -Frinny

          Comment

          • tomasjons
            New Member
            • Apr 2009
            • 7

            #6
            Thank you for your answare Frinny.

            I create my columns (dynamically) and records to a datatable. Then I fill the datatable with a query. Then on GridView.RowDat aBound I fill some textboxes with e.Row.Cells(j). Controls.Add(te xtbox). I can also assign a value to these textboxes. So far so good.

            Then I have a button outside the gridview witch fires SaveValues Sub.
            I use Gridview.Findco ntrol(testbox) to try to retrieve the data from the textboxes, but I always get the error: Object reference not set to an instance of an object.

            Everything looks right, but I can't seem to get it to work.

            Any tips?

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              When are you doing your DataBind?

              Comment

              • tomasjons
                New Member
                • Apr 2009
                • 7

                #8
                In the sub where I make the datatable with all rows and columns, but not the textboxes. I create the textbox on RowDataBound and fill it up like this:
                Code:
                 e.Row.Cells(j).Controls.Add(xTextBox)
                                Dim dsAttributter As DataSet = New DataSet
                                dsAttributter = objStatus.GetDataSet("SELECT attVerdi FROM xtemp_funkogkaptabell WHERE ProdID = '" & e.Row.DataItem("ProdID") & "' AND attNavn = '" & GridView1.HeaderRow.Cells(j).Text & "' ")
                
                                If Not dsAttributter.Tables(0).Rows.Count = 0 Then
                                    xTextBox.Text = dsAttributter.Tables(0).Rows(0)("attVerdi")
                                    xTextBox.ID = GridView1.HeaderRow.Cells(j).Text
                
                
                                Else
                                    xTextBox.Visible = True
                                End If

                No databind after that. Should I use databind somewhere in there? As you might guess, I am a bit new to all this :)

                Thank you very much for your time and effort.

                Hoping for a solution :)

                Tomas
                Last edited by Frinavale; Apr 21 '09, 02:16 PM. Reason: Added code tags. Please post code in [code] [/code] tags.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Well consider what happens when you do a DataBind() on a GridView.
                  The GridView is Bound to the data source.

                  If you do your DataBind too early, say in your Page Load event, then the data the the user entered will be lost because the GridView will be rebound to the "old" data.

                  This means that if the user was editing (before you did your DataBind), that information will no longer be available to you.

                  Because of this, I recommend that you preform your DataBind event at the latest point in the page's life cycle: in the PreRender event.

                  The other thing to consider is whether or not the TextBoxes are available in your SaveValues method....are they?

                  Comment

                  • tomasjons
                    New Member
                    • Apr 2009
                    • 7

                    #10
                    The textboxes are not available in my SaveValues method it seems.
                    I have tried out everything I can think of with the GridView.FindCo ntrol. I always get the same error.

                    So, I think my problem is that the TextBoxes are not available in my SaveValues method.

                    Puh :)

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      Sorry I should have been looking at your code.
                      I see that you are dynamically adding a TextBox to your GridView...and that you aren't actually using TemplateFields.

                      This is likely you problem.

                      There is so much to tell you about TemplateFields.
                      I think it would be best for you to research these before continuing with your solution.

                      Start by using the MSDN Library as your resource. Search for keywords like TemplateField, ITemplate, Templated Controls .... that should probably give you what you're looking for.

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        Originally posted by tomasjons
                        The textboxes are not available in my SaveValues method it seems.
                        I have tried out everything I can think of with the GridView.FindCo ntrol. I always get the same error.

                        So, I think my problem is that the TextBoxes are not available in my SaveValues method.

                        Puh :)
                        Yeah it has to do with the way that you are adding the TextBoxes to the GridView.

                        You think that the TextBoxes will be available later but in reality, when the GridView is recreated during the next request these TextBoxes are not part of the GridView.

                        You can't actually use the GridView.Find() method to find the TextBox because it's not part of the GridView.

                        It would be part of the GridView had you used a TemplateField though :)

                        Comment

                        • tomasjons
                          New Member
                          • Apr 2009
                          • 7

                          #13
                          Ok, thank you very much for your help. I will find out more about template fields.

                          Have a nice day.

                          Tomas

                          Comment

                          • tomasjons
                            New Member
                            • Apr 2009
                            • 7

                            #14
                            I have been "all over" the Internet. Can't seem to find a way to add textbox to gridview TempleteField from code behind. Think I have to find another way of doing this.

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              You can do it.
                              It's not that bad.

                              What have you tried so far?
                              Have you tried something like:
                              Code:
                              For i As Integer = 0 To dsAttributter.Tables(0).Columns.Count - 1
                                 'Creating a new TemplateField column
                                  Dim tf As New TemplateField
                              
                                  Dim item As New MyGridViewTemplate( _
                                                            DataControlRowType.DataRow, _
                                                            dsAttributter.Tables(0).Rows(0)("attVerdi"))
                              
                                  tf.ItemTemplate = item
                              
                                  myGridView.Columns.Add(tf)
                              Next


                              Where the MyGridViewTempl ate Class would be your own custom class that implements the ITemplate interface and defines what is in your template (see this):

                              Code:
                              Public Class MyGridViewTemplate
                                  Private _textBoxText As String
                                  Private _templateType As DataControlRowType
                              
                                  Public Sub New(ByVal type As DataControlRowType, ByVal text As String)
                                      _textBoxText = text
                                  End Sub
                              
                                     Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
                                      Select Case _templateType
                                              Case DataControlRowType.Header
                              'Defines what happens when applied to a header
                                                  Dim headerText As New Label
                                                  headerText.Text = _textBoxText
                                                  container.Controls.Add(headerText)
                                              Case DataControlRowType.DataRow
                              'Defines what happens when applied to a DataRow
                                                  Dim contentText As New TextBox
                                                  contentText .ID= "textBoxText"
                                                  contentText .Text = _textBoxText
                                                  container.Controls.Add(headerText)
                                       End Select
                              Please note that the above posted Loop has to be done in the PageInit event.
                              So in your PageInit you need to retrieve the data from the database and dynamically create the template fields for the GridView. This has to be done before the content is loaded into the GridView and before the GridView's ViewState is loaded (has to be done in the PageInit event).

                              Comment

                              Working...