sherbug1015
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(Sessi on("Cadenc eID"))) (stored proc call)
For Each col As DataColumn In dt.Columns
Dim bfield As New TemplateField()
bfield.HeaderTemplate = New GridViewTemplate(ListItemT ype.Header , col.ColumnName, isEditMode)
bfield.ItemTemplate = New GridViewTemplate(ListItemT ype.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
<asp:gridview id=gvcadencegrid runat="server" AutoGenerateColumns="false
<columns >
</columns>
</asp:gridview>
This is how I am filling the grid:
Protected Sub BindCadenceGrid()
Dim dt As New DataTable
dt = lCategory.LoadCadenceGrid(
For Each col As DataColumn In dt.Columns
Dim bfield As New TemplateField()
bfield.HeaderTemplate = New GridViewTemplate(ListItemT
bfield.ItemTemplate = New GridViewTemplate(ListItemT
gvcadencegrid.Columns.Add(
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
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.
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Open in new window