Link to home
Create AccountLog in
Avatar of sherbug1015
sherbug1015Flag for United States of America

asked on

Need to Save to Database from GridView with Dynamic Columns

I have a gridview with dynamic columns.  I am using a class that implements ITemplate.  The markup for the gridview looks like the following and there are no hardcoded column names

<asp:gridview id=gvcadencegrid runat="server" AutoGenerateColumns="false" width="10%"  >
            <columns >
     
            </columns>
        </asp:gridview>


This is how I am filling the grid:

Protected Sub BindCadenceGrid()
        Dim dt As New DataTable
        dt = lCategory.LoadCadenceGrid(CInt(Session("CadenceID")))   (stored proc call)
     

        For Each col As DataColumn In dt.Columns
            Dim bfield As New TemplateField()

            bfield.HeaderTemplate = New GridViewTemplate(ListItemType.Header, col.ColumnName, isEditMode)

            bfield.ItemTemplate = New GridViewTemplate(ListItemType.Item, col.ColumnName, isEditMode)

            gvcadencegrid.Columns.Add(bfield)
        Next


        gvcadencegrid.DataSource = dt


        gvcadencegrid.DataBind()
    End Sub

After calling the class the grid fills with all rows and columns as expected.  The problem is the user is able to make changes to the textboxes created and save the data back to the database.  

My question is how would I save the data back to the database?  I have looked around, but can't find anything that addresses saving data from a gridview with dynamic columns.    There are plenty of articles on how to save when there are hardcoded columns, but nothing for dynamically created columns.  Hopefully someone has done this before.

I have attached the code from the class and a screen shot of the grid.  

Thanks
editablegrid.jpg
GVTemplate.txt
Avatar of nost2
nost2

You can loop through the grid view and find values for all of the text boxes like this:

    Protected Sub Unnamed_Click(sender As Object, e As EventArgs)
        Dim tb As TextBox
        For Each row As GridViewRow In gvcadencegrid.Rows
            For Each Cell As TableCell In row.Cells
                tb = Cell.Controls(0)
            Next
        Next
    End Sub

Open in new window

Avatar of sherbug1015

ASKER

nost2 -

 I have already tried this.  There are no rows in the gridview as they are being created dynamically.  I am trying to find a way to get a reference to the rows that are created dynamically.
ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer