469,607 Members | 2,039 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,607 developers. It's quick & easy.

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.FindControl("NAME")).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
Dec 26 '08 #1
14 14804
7,872 Expert 4TB
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.
Dec 30 '08 #2
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.

Jan 9 '09 #3
Hi agamoto

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

Apr 21 '09 #4
9,735 Expert Mod 8TB
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.

Apr 21 '09 #5
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.RowDataBound I fill some textboxes with e.Row.Cells(j).Controls.Add(textbox). 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.Findcontrol(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?
Apr 21 '09 #6
9,735 Expert Mod 8TB
When are you doing your DataBind?
Apr 21 '09 #7
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:
Expand|Select|Wrap|Line Numbers
  1.  e.Row.Cells(j).Controls.Add(xTextBox)
  2.                 Dim dsAttributter As DataSet = New DataSet
  3.                 dsAttributter = objStatus.GetDataSet("SELECT attVerdi FROM xtemp_funkogkaptabell WHERE ProdID = '" & e.Row.DataItem("ProdID") & "' AND attNavn = '" & GridView1.HeaderRow.Cells(j).Text & "' ")
  5.                 If Not dsAttributter.Tables(0).Rows.Count = 0 Then
  6.                     xTextBox.Text = dsAttributter.Tables(0).Rows(0)("attVerdi")
  7.                     xTextBox.ID = GridView1.HeaderRow.Cells(j).Text
  10.                 Else
  11.                     xTextBox.Visible = True
  12.                 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 :)

Apr 21 '09 #8
9,735 Expert Mod 8TB
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?
Apr 21 '09 #9
The textboxes are not available in my SaveValues method it seems.
I have tried out everything I can think of with the GridView.FindControl. I always get the same error.

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

Puh :)
Apr 21 '09 #10
9,735 Expert Mod 8TB
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.
Apr 21 '09 #11
9,735 Expert Mod 8TB
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 :)
Apr 21 '09 #12
Ok, thank you very much for your help. I will find out more about template fields.

Have a nice day.

Apr 21 '09 #13
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.
Apr 22 '09 #14
9,735 Expert Mod 8TB
You can do it.
It's not that bad.

What have you tried so far?
Have you tried something like:
Expand|Select|Wrap|Line Numbers
  1. For i As Integer = 0 To dsAttributter.Tables(0).Columns.Count - 1
  2.    'Creating a new TemplateField column
  3.     Dim tf As New TemplateField
  5.     Dim item As New MyGridViewTemplate( _
  6.                               DataControlRowType.DataRow, _
  7.                               dsAttributter.Tables(0).Rows(0)("attVerdi"))
  9.     tf.ItemTemplate = item
  11.     myGridView.Columns.Add(tf)
  12. Next

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

Expand|Select|Wrap|Line Numbers
  1. Public Class MyGridViewTemplate
  2.     Private _textBoxText As String
  3.     Private _templateType As DataControlRowType
  5.     Public Sub New(ByVal type As DataControlRowType, ByVal text As String)
  6.         _textBoxText = text
  7.     End Sub
  9.        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
  10.         Select Case _templateType
  11.                 Case DataControlRowType.Header
  12. 'Defines what happens when applied to a header
  13.                     Dim headerText As New Label
  14.                     headerText.Text = _textBoxText
  15.                     container.Controls.Add(headerText)
  16.                 Case DataControlRowType.DataRow
  17. 'Defines what happens when applied to a DataRow
  18.                     Dim contentText As New TextBox
  19.                     contentText .ID= "textBoxText"
  20.                     contentText .Text = _textBoxText
  21.                     container.Controls.Add(headerText)
  22.          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).
Apr 22 '09 #15

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Robert Smith jr. | last post: by
3 posts views Thread by NateDawg | last post: by
1 post views Thread by Gregory Gadow | last post: by
2 posts views Thread by =?Utf-8?B?dmFuZGls?= | last post: by
4 posts views Thread by Mark Olbert | last post: by
2 posts views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.