.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Tricky Insert Logic for dynamically created row in GridView.

Posted By:      Posted Date: October 20, 2010    Points: 0   Category :ASP.Net

Hi, and thanks for peeping the query i have.

I am dynamically generating the Row in GridView and saving the data in database using StringCollection. Instead of using StringCollection i want to pass the data(user entered data) to local variable , eg:

string name = txtFName; and so on. but it wont work for dynamically created rows.

So if you knw how to assign the user provided data in the local variable like above i have show, please help me out. 

The code for inserting the records for dynamic created rows in grid view via StringCollection is below


protected void btnSave_Click(object sender, EventArgs e)


        int rowIndex = 0;

        StringCollection sc = new StringCollection();

        if (ViewState["CurrentTable"] != null)


            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];

            if (dtCurrentTable.Rows.Count > 0)


View Complete Post

More Related Resource Links

how to apply validation controls on Gridview which is created dynamically

 hi, i created a gridview with two columns and 8 rows dynamically. All the rows are empty and editable at runtime. I am giving column names for this grid from some other .aspx page by passing column names using querystring.Because i am using this grid for three different pairs of column names.  i would like to know how to apply Required Field Validator, Regular Expression Validator  and Range Validator on Gridview textboxes. So that by using those validators i want to validate user inputs. And i would also like to know how to add rows at runtime for this grid. I am using ASP.net 2.0 and C#.net for this.(Visual Studio 2005) The code i am using to create gridview is below. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoadData.aspx.cs" Inherits="LoadData" %>  <form id="form1" runat="server">  <div> <asp:GridView ID="GrdDynamic" runat="server" AutoGenerateColumns="False" ForeColor="#333333" BorderColor="GradientInactiveCaption" BorderStyle="Solid" style="z-index: 108; left: 24px; position: absolute; top: 13px">  <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  <RowStyle BackColor="#EFF3FB"

How to use dynamically created texbox and labels during insert


With the help of other members i was able to add a table dynamically to my form.(http://forums.asp.net/t/1607009.aspx)

Now i need to grab the values and insert into the database. Below is the code used to create the table when a selection is made from a dropdown. My insert expects 3 parameters, ID, CamID, CamName

The ID will be from a field in the form, the CamID needs to be the "j" value below and the Camname to be the txtcam value.

What exactly can i do or what do i search on for help with this?



            for (int i = 1; i <= Convert.ToInt32(ddlNumCam.SelectedValue); i = i + 3)
                HtmlTableRow row = new HtmlTableRow();
                for (int j = i; j < i + 3 && j <= Convert.ToInt32(ddlNumCam.SelectedValue); j++)
                    HtmlTableCell cell1 = new HtmlTableCell();
                    HtmlTableCell cell2 = new HtmlTableCell();
                    cell1.Controls.Add(new TextBox() { ID = "txtCam" + j });
                    cell1.Width = "315px";
                    cell2.Controls.Add(new Label() { ID = "lblCam" + j, Text = " Camera # " + j + "  " });
                    cell2.Width = "115px&

Gridview Paging with Dynamically created Gridview in VB.NET



I have a page that I am creating 5 separate GridViews on, depending on the button the user clicks, a different GridView will show up. On a few of the GridView's there is a need for paging, so in the CodeBehind I did the following

            billingGridView.ID = "BillingGridView"
            billingGridView.DataSource = sdsBilling
            billingGridView.AutoGenerateColumns = False
            billingGridView.AllowPaging = True

            billingGridView.ID = "BillingGridView"
            billingGridView.DataSource = sdsBilling
            billingGridView.AutoGenerateColumns = False

Gridview SelectedIndexChanged on a dynamically created gridview


my html page is basically a panel

Programmatically i've created the entire page adding controls to the panel. I have a dynamically created Accordion and within each accordion pane i have a dynamic gridview based on the properties for the accordion pane. Each row within the GridView i have a Button with the CommandName of Select, with this i want to open a new window and go to a new URL with the properties in a BoundField. On postback i want to be able to return to the Accordion.SelectedIndex. 

The problem is i can't seem to use a findControl to capture the index of either the GridView or Accordion. Below is my code:

AjaxControlToolkit.Accordion accordion = new AjaxControlToolkit.Accordion();
        accordion.ID = "Accordion2";
        accordion.HeaderCssClass = "accordionHeader";
        accordion.ContentCssClass = "accordionContent";
        accordion.HeaderSelectedCssClass = "accordionHeaderSelected";
        accordion.FramesPerSecond = 100;
        accordion.TransitionDuration = 100;
        accordion.FadeTransitions = true;
        accordion.EnableViewState = true;
        if (Session["myAccordion.SelectedIndex"] != null)
            accordion.SelectedIndex = (int)Session["myAccordion.SelectedIndex"];


Insert Rows with a GridView

In ASP.NET version 1.x, I used the footer on the DataGrid control to insert new rows by placing a series of TextBox, DropDownLists, CheckBoxes etc. controls on it and then handling a save button. This also works with the GridView in version 2.0 but with one major exception: if your data source is empty, the GridView will hide your footer (and header) and only display the EmptyDataText.

ListBox tricky to set in a DataGrid/GridView

Listboxes are very tricky to set selected items in grid - there is no real nice way to do it. It would have been great if Microsoft included a property that accepts an array or comma separated values - or anything! But they only allow each item in the list to be selected individually. The way to do this in a grid is a bit messy, but it is fairly simple to implement. When a row is created (as per the event), select the items of the listbox as per the datagrid source data:

insert the data in database using gridview jquery



I want to use gridview and bind the data and after i want insert the data in database using jquery,

below the link i got for delete


but i want the sample example for insert  row from gridivew and add the database.


Custom Grid View Header - Grid view Row Created event vs Gridview row databound events ?


Hi Gurus ,

It happened to add an extra Gridveiw Header in row_databound event , It did worked fine on !Postback but disappered on Page.Postback . Quick google search guided me to move the event to Row_Created event and every thing is okay .

Can any expert post some pointers , differnces between grid row_created vs row_databount with some sample table data created dynamically  behaviour of both the events in !Postback and page.Postback .

Thanks a tonne in Advance and  any inputs will be valued and appreciated .

Insert data based on gridview user value on row.. how to



I have three column comes from database table(Table1) and one column is for entering data for the user let us consider it look like this 

ItemNo ItemName Qty  uservalue

001       A               50       20

002       A               20       5

003       B               50  

004       C               60      10

005       D               40 

006       E               90      15 


If its 300 row from database to gridview it will show 50 record with page navigation..

What i need is i need to insert data in to database table which gridview row has user value..

The output of the another table(Table2) will look like this after insert

ItemNo ItemName   uservalue

001       A                    20

002       A                      5  

Adding carriage returns between 2 dynamically created controls in an HtmlTableCell


The title pretty much explains it. Here is what i'm trying to do...

 Private Sub LoadGrid_CommentsField(ByVal Row As HtmlTableRow, ByVal BookingNumber As String, ByVal CommentsText As String)
  Dim Txt As TextBox
  Dim Cmd As ClickOnceButton.ClickOnceButton

  Txt = New TextBox
  Txt.ID = "txtComment_" & BookingNumber
  Txt.CssClass = "TFAppControls"
  Txt.TextMode = TextBoxMode.MultiLine
  Txt.MaxLength = 500
  Txt.Width = New Unit(95, UnitType.Percentage)
  Txt.Text = CommentsText

  Row.Cells(cComments).InnerHtml &= "<br/>"

  Cmd = New ClickOnceButton.ClickOnceButton
  Cmd.DisableAfterClick = True
  Cmd.Text = "Save Comments"
  Cmd.ID = "cmdSaveComments_" & BookingNumber
  Cmd.CssClass = "TFAppButtons"
  Cmd.EnableViewState = False
 End Sub

On the line where i try and update the InnerHtml, i get the following error; "Cannot get inner content of  because the contents are not literal."

Dynamically creating Gridview Template - Linkbutton Click not working



I have written some code to dynamically generate template columns for gridview which works well. However, each cell in the gridview has to be a linkbutton, which when clicked does a db update and redirects to a specific url with some parameters in it.

I have attached a click event handler to the linkbutton in the InstantiateIn method but the event does not seem to fire.

Could someone please help? the code is below -

//Dynamically creating the Grid

protected void btnAnalyze_Click(object sender, EventArgs e)

hsdsd: how to insert two values from text box1 to gridview g



table a

a                                 b

jhgd                           kljhskjd

sdghdshgd                 jkhdjhdjk

what i need is when enter some values(varchar ) in txt 1 and 2

it should be added in database and shown in gridview1 after i press butt1


any bodyknows full code answer/akdj

Issues with dynamically created TabContainer (Hide/Show TabPanel)


I have a web page that contains an UpdatePanel. In that panel, there is a TabContainer control that has 5 TabPanels, only the first one "Tab 1" will be displayed during initial page load. There is an Add button in "Tab 1", whenever it gets clicked, I will add (show) the next TabPanel (Tab 2, Tab 3, etc., all the way to Tab 5). "Tab 2" thru "Tab 5" contains a Close button that will close (hide) the current tab when clicked.

I'm doing all of these from code behind (dynamically create controls in Page_Init event and re-generate them during postback, track each TabPanel's content in a session variable, etc.), and everything seems to work just fine IF after adding several TabPanels, I remove  them in order (i.e. remove Tab 5 first, then Tab 4, Tab 3, etc.). It will NOT work whenever I try to remove a tab in the middle, then I will get all kinds of strange results.

I've attached the sample codes here for reviewing and testing. Any help is greatly appreciated.


==================== demo.aspx page ====================

<%@ Page

How can i add a textbox into a GridView Cell dynamically and how can i retrieve entred data

Hey, i think that the the subject can explain what i need, it's about adding a TextBox control with the code behind into a GridView, and also about how can i retrieve entered data after submitting, im using C#,Thanks.

how to add textbox dynamically in a gridview

i have to create several textbox inside a gridview with its textboxid unique. can anyone tell me how to achieve this with a code in Asp.net 2.0 ?

How to fetch data from textbox dynamically in a gridview

How to fetch data from textbox inside a gridview

Insert columns into GridView and rendercontrol error

Hi I´m inserting columns into a gridview at run time using: gvRageReport.Columns.Insert(index, column) When I render the gridview in orther to export to excel all the excel columns are empty except the new insert columns, but when I add the new columns using gvRageReport.Columns.Add(column) the excell is exported successfully.Any idea?
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend