Geeks With Blogs

News

Microsoft MVP


Moderator at CodeASP.NET


Quiz Master




free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

Introduction:

I decided to write this article because this has been asked so many times before at the forums(http://forums.asp.net) . Basically, this example shows on how to generate a Row in GridView with TextBoxes when clicking a Button that is residing inside the GridView footer.

 

To get started, let’s grab a GridView control from the Visual Studio Toolbox and put it in the WebForm. The mark up would look something like this:

 

<asp:gridview ID="Gridview1" runat="server" ShowFooter="true"
                             AutoGenerateColumns="false">
        <Columns>
        <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
        <asp:TemplateField HeaderText="Header 1">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Header 2">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Header 3">
            <ItemTemplate>
                 <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
                 <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" />
            </FooterTemplate>
        </asp:TemplateField>
        </Columns>
</asp:gridview>

 

 

Since this demo is intended to generate rows of TextBoxes in GridView, then we set up some TemplateFields columns so that GridView will automatically generates TextBoxes when a new row is being added.

 

As you can see I have set up a BoundField Column for displaying the RowNumber and sep up three TemplateField Columns in the Grid and added each columns a TextBox Control. You would also noticed that I have added a Button Control under the FooterTemplate at the last column in the GridView.

 

Note: Since we are added a Control in the GridView footer, then be sure to set ShowFooter to TRUE in the GridView.

 

Now let’s switch to the Code behind part of the webform.

 

As you may know, the GridView control will not show in the page once there is no data associated on it. So the first thing we need here is to set an initial data in the  GridView  Control. To do this, we can use a DataTable for binding our GridView.

 

Here’s the code block below:

 

private void SetInitialRow(){

        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();
}

  

As you can see, we defined four Columns in the DataTable called RowNumber, Column1, Column2 and Column3. The RowNumber column  will serve as the key for generating the rows in the GridView. Noticed that for Columns  1,2 and 3, I assigned an empty value for that columns since the GridView will be generated for the first time. You also noticed that I store the DataTable in ViewState so that we can reference the current data associated within the DataTable when it postbacks.

 

Now lets call the method above in Page_Load event:

 

protected void Page_Load(object sender, EventArgs e){
        if (!Page.IsPostBack){
            SetInitialRow();  
        }
}

 

Running the codes above will give us this output below:

 

 

Now let’s create the method for generating the rows when clicking the Button. Here are the code blocks below:

 

 

private void AddNewRowToGrid(){

        int rowIndex =0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1; 
                    drCurrentRow["Column1"] = box1.Text;
                    drCurrentRow["Column2"] = box2.Text;
                    drCurrentRow["Column3"] = box3.Text;

                    rowIndex++;
                }

                //add new row to DataTable
                dtCurrentTable.Rows.Add(drCurrentRow);
                //Store the current data to ViewState
                ViewState["CurrentTable"] = dtCurrentTable;

                //Rebind the Grid with the current data
                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
           }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
}

 

As a summary, the code above gets the previous data stored from the viewstate and set the data stored from it into a DataTable so that we can add a new row based from the value entered from the TextBox.

 

You will also noticed that we call the method SetPreviousData() at the bottom part of the codes above. Now where is that method?  Below are the code blocks for that method:

 

private void SetPreviousData(){

        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

 
                    box1.Text = dt.Rows[i]["Column1"].ToString();
                    box2.Text = dt.Rows[i]["Column2"].ToString();
                    box3.Text = dt.Rows[i]["Column3"].ToString();
                   
                    rowIndex++;

                }
            }
        }
}

  

Now, since the methods are all set then we can call this at Button Click event of the Button.

 

protected void ButtonAdd_Click(object sender, EventArgs e){
        AddNewRowToGrid();
}

 

 

As you can see the code above is very straight forward and self explanatory.  Running the code above will give us this output below:

 

 

 

That’s it! I Hope you will find this example useful!

 

Posted on Thursday, June 4, 2009 10:13 PM ASP.NET , C# , GridView | Back to top


Comments on this post: Adding Dynamic Rows in GridView with TextBoxes

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Good Article, thanks for giving us valuable things like this...


Regards
Avinash
Left by avinash on Jun 04, 2009 11:49 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
hi,

great article. but my sceniro is i need two gridview with emptyrows and the gridview should be generated dynamically for fast rendering of the page.

i hope you help me

Left by senthil on Jun 27, 2009 6:51 AM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
it really helped
thanks:-)
Left by surabhi on Jun 29, 2009 1:12 AM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
hi!
Its gud! It works...
Thnx
Left by Neha on Aug 03, 2009 5:56 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
good article for beginners!
Appreciate your good work.
Left by Viru on Aug 26, 2009 1:45 AM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
do you have the code to retreive the data from the text boxes after you have saved data into it.
Left by Great on Oct 23, 2009 2:30 AM

# Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
In my case I have the grid view bound to a sqldatasource. How can I dynamically add new rows now
Left by Sanjay Krishnamurthy on Feb 03, 2010 7:31 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Works Great.. Excellent. was able to submit to database also.
Couldn't figure out how to add a running total in the footer though..
Left by Jayman on Feb 05, 2010 2:38 AM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
where i can see the database table
if i insert data data is not entering
Left by samson on Feb 05, 2010 10:21 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Code works fine for me, but how to refresh the viewstate of the datatable?

After generating a new row with some values on the textboxes on the datagrid, if we make changes to the first row data it gets changed to the initial value!!

Require your help on this VINZZ!!!!!!

Thanks for your help in advance!!!!!

Ashiq
Left by Mohamed Ashiq on Mar 03, 2010 10:15 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Problem that I am having is that when I go to update data in a previous row and then add a new row - updated data in textbox is lost and just always reverts to data in the viewstate?
Left by Ashiq on Mar 03, 2010 10:26 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Good article
but how to update and delete in grideview
Left by Ahmed on Mar 04, 2010 3:18 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Thank you
I Have another question
how to add this data in MS sql after adding many rows not one by one
Left by Ahmed on Mar 08, 2010 4:30 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Thank you
How to make add new row limited
Left by Ahmed on Mar 08, 2010 7:18 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
how to remove particular row with this can we
Left by priya on Mar 20, 2010 8:00 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Good one.

But I have checkbox and dropdownlist box also. Page load gets datafrom database and it will show on grid view but at the end want the user can enter values by clicking on add button. so when click add new row should have get all the controls on my gridview and again at footer add button and they click add new row creates. At the end I have save button where it save all the entries user had done it.

Is it possible using asp.net in c#?
Left by desivir on May 24, 2010 1:07 AM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Nice one i implemented the same with same point its wonder article you have shared..

keep the good work
thanks
Akshaya
Left by Akshaya on Jun 16, 2010 6:11 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
I will aprciate your work.I used ur code .Thanking you.If posile can describe how to work this with database.
Left by vinod on Jun 18, 2010 3:57 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Nice Article and demo.
I got solution from this link.
Left by Nilesh Chaudhari (caGloo) on Jun 29, 2010 8:52 PM

# How To Save All this values to Database?
Requesting Gravatar...
Its very good articles...any one can help me to save all this values in database
Left by Devang on Jul 13, 2010 2:50 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
The code is working very fine.It is very useful for us.Thanks a lot.
Left by Dhanalakshmi,Saravanan on Aug 17, 2010 3:48 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
thanks , that is very useful .....
keep the good work
:)
Left by stuart on Aug 24, 2010 5:07 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Simply brillant
Left by Ty on Aug 25, 2010 6:37 AM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Great Coding
Left by A on Sep 04, 2010 7:46 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Hi,

Seriously.. ur code has helped alot. Thanks for writing such useful posts.
Left by Pooja on Sep 16, 2010 2:54 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Nic One But i did it some thing diffrent i Create a gridview as U created but for adding New row i used java script coding like
function AddNewRecord(keyCode, obj)
{
if (window.event.keyCode==13 )
{
var grd = document.getElementById('Gridview1');
var tbod=grd.rows[0].parentNode;
var newRow=grd.rows[grd.rows.length - 1].cloneNode(true);
cleanUpInputs(newRow);
tbod.appendChild(newRow);
return false ;
}
}

every thing fine but as i goes to server side and trying to capture text data To Any String Var Like

for (int i = 0; i < Gridview1.Rows.Count; i++)
{

TextBox Box1= (TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox1");
}

Its not working means
Control not entered iinside loop
i f i change Gridview.row.count to simply 5 its show an error to idntify the control.

Please
Help me out

I used only setinitialdata() funtion from ur seniario.


Left by Ashish on Oct 29, 2010 3:43 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
hi,
i am able to generate new row with the help of yr topic but my data is lost when new row created previous row data lost.
i am not able to store these value in session and again session to database if u have any sample source code program plz let me know.
Left by shivesh on Nov 10, 2010 7:56 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
your logic is working f9 but creates problem when i tried to delete the row at delete command.i found that basically the top most row is always found empty.any solution plz...
Left by pariksit on Dec 31, 2010 12:52 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
works perfect,Thanks :)
Left by Enis on Feb 01, 2011 6:24 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Could you pls provide code for inserting those multiple rows at a time into database and bind to gridview when i press insert button
Left by Prasanthi on Feb 01, 2011 6:25 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
@Prasanthi,
Look here: http://geekswithblogs.net/dotNETvinz/archive/2009/08/02/save-dynamic-textbox-values-from-gridview-to-database.aspx
Left by Vinz on Feb 01, 2011 7:26 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
My english is not good but I will try to explain my problem.
I tried to delete a row with rowdeleting event, but I couldn't,I think you stored data in viewstate.Can you tell me a solution about that ?

thanks
Left by Pendamir on Feb 02, 2011 4:49 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
great work ,its working perfectly
Left by varaprasad on Feb 17, 2011 7:48 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Thanks a ton......this article was very helpful :)
Left by hima on Feb 22, 2011 7:53 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Good Article Thanks
Left by punitha on Mar 03, 2011 8:17 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Good It is very Useful
Left by punitha on Mar 03, 2011 8:24 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Hi It works fine... Good Work
Could u plz tell me how to implement code for deleting row
Left by Arch on Mar 10, 2011 2:25 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
thank you very much
i got your code for dynamically creating text boxes
Left by ABHITOSH KUMAR on Mar 29, 2011 6:05 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
This is very help less Comment or reply
Left by DEmons on Apr 04, 2011 8:28 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Code works fine for me, but how to refresh the viewstate of the datatable?

After generating a new row with some values on the textboxes on the datagrid, if we make changes to the first row data it gets changed to the initial value!!

Require your help on this.

Thanks for your help in advance!!!!!
Left by Shivani on Apr 15, 2011 2:56 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
@Shivani,

Please refer this post for the updated codes:
http://geekswithblogs.net/dotNETvinz/archive/2009/08/12/updated-adding-dynamic-rows-in-asp.net-gridview-control-with-textboxes.aspx
Left by Vinz on Apr 15, 2011 5:20 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
It really worked ... thanks alot.
Left by Cheta on Apr 21, 2011 9:40 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Will you please tell me how to add required field validation to the 2nd row 3rd row .... n row.

on btn click next which is redirecting to next page.


thank you for this article post..
Left by Abuhurera on May 13, 2011 9:20 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
@Abuhurera,

This should help you to get started:

http://geekswithblogs.net/dotNETvinz/archive/2010/08/05/adding-dynamic-rows-in-gridview-with-textbox-and-dropdownlist-again.aspx
Left by Vinz on May 13, 2011 10:25 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Great sir ji....
realy this code is working....
Nice Work, keep it up!!!
Again , thanks a lot

With regards
vivek sharma
Left by vivek on May 14, 2011 1:44 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Your article is very useful but if i want to remove any of the row from above generated dynamic grid view. viz. If i add a new row and afterward i need to delete that row from the grid view. How can i do that dynamically?
Left by Paras on May 18, 2011 12:46 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
@Paras,

Look here: http://geekswithblogs.net/dotNETvinz/dotNETvinz/archive/2009/08/12/updated-adding-dynamic-rows-in-asp.net-gridview-control-with-textboxes-again.aspx
Left by Vinz on May 18, 2011 1:48 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Awesome man,

Excellent article..... It works fine to the Core... Perfect...

Thanks
T.Vinoth
Left by Vinoth on May 26, 2011 4:57 AM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Hi VInz,

Excellent article...but if i need to delete a row how do i do it?
Left by Kunal on Jun 13, 2011 11:15 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
@Kunal,

You can refer this other post at: http://geekswithblogs.net/dotNETvinz/dotNETvinz/archive/2009/08/12/updated-adding-dynamic-rows-in-asp.net-gridview-control-with-textboxes-again.aspx
Left by Vinz on Jun 14, 2011 1:25 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
how to delete the dynamically added rows?
Left by s.varun on Jun 20, 2011 3:56 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
TO ALL:

FOR DELETION:

http://geekswithblogs.net/dotNETvinz/dotNETvinz/archive/2009/08/12/updated-adding-dynamic-rows-in-asp.net-gridview-control-with-textboxes-again.aspx
Left by Vinz on Jun 20, 2011 4:55 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Its Very nice
thanks

PRAKASH
The Boss
Left by Prakash on Jun 25, 2011 8:22 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
//Declaring Global variable datatable and dataset
DataTable dt = new DataTable();
DataSet Gds = new DataSet();

protected void Page_Load(object sender, EventArgs e)
{
//adding Columns when Page Load
dt.Columns.Add("ExpId", typeof(int));
dt.Columns.Add("FirstName", typeof(string));

}

//Asp Button Click BtnLoad
protected void BtnLoad_Click(object sender, EventArgs e)
{
// gvLoad is Grid View Id

if (gvLoad.Rows.Count == 0)
{ //At first time loading Gridview
dt = tblLoad();
}
else
{ //At second time loading Gridview
dt = tblGridRow();
dt.Rows.Add(tblRow());
}
//Binding Datasource of Gridview
databind();

}

protected DataTable tblLoad()
{ //adding rows in datatable
dt.Rows.Add(tblRow());
return dt;
}
protected DataRow tblRow()
{
//loading rows
DataRow dr;
dr = dt.NewRow();
dr["ExpId"] = Convert.ToInt16(txtId.Text); //asp textBox value
dr["FirstName"] = Convert.ToString(txtName.Text); //asp textBox Value
return dr;
}

protected DataTable tblGridRow()
{// loads datatable from Gridview
DataRow dr;
for (int i = 0; i < gvLoad.Rows.Count; i++)
{
dr = dt.NewRow();
dr["ExpId"] = Convert.ToInt16(((Label)(gvLoad.Rows[i].Cells[0].FindControl("lblGcheck1"))).Text);
dr["FirstName"] = ((Label)(gvLoad.Rows[i].Cells[1].FindControl("lblGcheck2"))).Text;
dt.Rows.Add(dr);



}
return dt;
}
void databind()
{
gvLoad.DataSource = dt;
gvLoad.DataBind();
Session["dtbl"] = dt;
}
protected void btn_Click(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx");
}

protected void gvLoad_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//deleting selected rows
dt = tblGridRow();
dt.Rows.RemoveAt(e.RowIndex);
databind();
}
protected void gvLoad_RowEditing(object sender, GridViewEditEventArgs e)
{
//editing selected rows
gvLoad.EditIndex = e.NewEditIndex;
tblGridRow();
databind();

}
protected void gvLoad_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Updating selected rows
dt = (DataTable)Session["dtbl"];
dt.Rows[e.RowIndex]["ExpId"] = ((TextBox)(gvLoad.Rows[e.RowIndex].Cells[0].FindControl("txtGTest1"))).Text;
dt.Rows[e.RowIndex]["FirstName"] = ((TextBox)(gvLoad.Rows[e.RowIndex].Cells[1].FindControl("txtGTest2"))).Text;
gvLoad.EditIndex = -1;
databind();
}
protected void gvLoad_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Cancelling edit
gvLoad.EditIndex = -1;
tblGridRow();
databind();
}



}
Left by JOEL LYTE on Jun 25, 2011 8:54 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Great work :)
Left by Disha on Jul 01, 2011 11:10 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Hi,

Can you please tell me how to add 5 blank rows in grid at page load.

Thanks
Left by Anuj on Jul 07, 2011 2:35 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
@Anuj,

Just add additional rows in the SetInitialRow() method and assign each row value to empty.
Left by Vinz on Jul 07, 2011 7:02 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Pls help to solve below problem* :

1. First to diplay grid with 5 blank rows in page load.

2. After inserting value in all five rows when i click Addnewrow button i have to add new blank row below fifth one.

3. When the new row is bind in grid the above five rows inserted textboxes value are to be as it is (not refresh or empty).

First 2 problem are solved by your given code but last one not.

Pls reply..
Left by Anuj on Jul 07, 2011 9:05 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
@Anuj,

You can tweak the SetPreviousData() method to conform with your needs. Just debug your code and step into it so that you'll figure out why it's not retaining the values. You may also want to look at this post for the updates:
http://geekswithblogs.net/dotNETvinz/dotNETvinz/dotNETvinz/archive/2009/08/12/updated-adding-dynamic-rows-in-asp.net-gridview-control-with-textboxes.aspx
Left by Vinz on Jul 07, 2011 10:47 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
this was very useful for me and i have one problem that i need the particular row value from the grid view if i put the value in 5th row number mean that value alone i need ..............................
Left by sampath on Jul 21, 2011 2:27 PM

# how to delete Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
I tried this from ur code....

protected void DeleteNewRowToGrid()
{
int rowIndex = 0;

if (ViewState["dtCurrentTable"] != null)
{
DataTable DeldtCurrentTable = (DataTable)ViewState["dtCurrentTable"];
DataRow drCurrentRow = null;
if (DeldtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i > DeldtCurrentTable.Rows.Count; i--)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
//TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("ddldatatype");

drCurrentRow = DeldtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i - 1;
drCurrentRow["Column1"] = box1.Text;
drCurrentRow["Column2"] = box2.Text;
//drCurrentRow["Column3"] = box3.Text;

rowIndex--;
}
//add new row to DataTable
DeldtCurrentTable.Rows.Remove(drCurrentRow);
//Store the current data to ViewState
ViewState["dtCurrentTable"] = DeldtCurrentTable;

//Rebind the Grid with the current data
Gridview1.DataSource = DeldtCurrentTable;
Gridview1.DataBind();
}
}
}

protected void ButtonDel_Click(object sender, EventArgs e)
{
DeleteNewRowToGrid();
}
Left by Mahendra on Aug 27, 2011 5:24 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
It is very nice coding.. but for deletion of row code and you dynamic generation of tetxbox coding should be at one place.. I tiered for searching this code fro delete row from textbox.. But at last I got code which u you implemented for dynamic generation of tetxbox.. URL for Deleting row.. Please Keep these two thing together not seperate
URL: http://codeasp.net/articles/asp-net/112/adding-dynamic-rows-in-asp-net-gridview-control-with-textboxes-and-with-delete-functionality
Left by Mahendra on Aug 29, 2011 7:12 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Hi, Question.
When my first row is displayed it has a really big height, I tried to set the height manually but it doesn't work, why is this happening and how can I repair this?

Thanks
Left by Eduardo on Sep 12, 2011 11:10 AM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
HI,

i dont understand how this setpreviousvalues() function works.. it does not assign to grid but still the values are restored in grid. if i comment this function then the grid is empty at the end of post back...!!

how this works..please expplain
Left by bharani on Sep 12, 2011 7:41 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Very helpfull! thanks!
Left by Nestor on Sep 13, 2011 6:25 AM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
this code is very helpful for me.but i want to add paging in it .please help me.
Left by Bhupesh Bisht on Sep 20, 2011 7:54 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Sir,

Good job but i have doubt Pl clear it

how to show the some integer value in text box while loading a page
Left by balakrishna on Sep 30, 2011 7:09 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
How to save all the rows entered by the user to sql table? plzz can u help us out
Left by Suchitra on Oct 01, 2011 11:40 AM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
i know this way ...if there are any other alternates?
Left by aseem on Oct 07, 2011 8:14 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Really good and very simple to understand...
Left by praveen on Oct 20, 2011 2:43 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Wow, that code was exactly what I'm looking for....OMG, thank you so much!
Left by kinnoy on Oct 25, 2011 4:19 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Thanks a ton..

good solution.. i like it.. very easier.. :)

Left by Niteen on Nov 17, 2011 3:43 PM

# re: Adding Dynamic Rows in GridView when command button click .
Requesting Gravatar...
Respected sir,

i am working in software developement.

sir, i need to logical program that use in project work . related data control , ajax.
Left by vishal.k.chhaya on Nov 23, 2011 1:46 AM

# re: Adding Dynamic Rows in GridView when command button click .
Requesting Gravatar...
Respected sir,

i am working in software

sir, i need to logical program that use in project work . related data control
Left by vishal.k.chhaya on Nov 23, 2011 1:47 AM

# re: Adding Dynamic Rows in GridView when command button click .
Requesting Gravatar...
Respected sir,

i am working in software

sir, i need to logical progr . related data control
Left by vishal.k.chhaya on Nov 23, 2011 1:48 AM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
help me in this!!

i need to have a textbox only at the newly added row..i shoul not use footer template........if am adding multiple no of rows, those rows alone should have textbox.....datum from DB should be in as labels in gridview.....
Left by idhris on Dec 13, 2011 8:07 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Great Article it helped me a lot. I have a new requirement where in the when the user click on "Add" button the textboxes should accept only numerics and show the sum of total column in the footer, i was able to attach javascript for the TextBox for Numerics, but i am facing problem to show the sum of the columns when user enters a value. can you pls guide me ( i am using gridview in content page within the ScriptManager, and when i try to get the ClientID of the textBoxes, i see that there is only one row (in view source), the new rows i am adding doenn't have any clientID generated.
Left by chaitanya on Dec 27, 2011 6:54 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
i want update the values in gridview plz solve my problem
Left by veenu on Jan 27, 2012 6:25 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
good one
Left by sindu on Feb 07, 2012 5:30 PM

# Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Respected sir,

i am working in software

sir, i need to find total of last columns of grid dynamically added textbox that total will be display in simple text box
Left by Vikas Patil on Apr 07, 2012 11:44 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
how can i save this data into database ???
Left by Rupesh on Apr 24, 2012 4:28 PM

# re: Adding Dynamic Rows in GridView with TextBoxes- This Worked for me
Requesting Gravatar...
private void addNewRowToGrid()
{

DataTable dt = new DataTable();
DataRow row = null;
dt.Columns.Add("CurrencyFormat");
dt.Columns.Add("ConversionRate");


foreach (GridViewRow grow in gridviewConversionRate.Rows)
{
row = dt.NewRow();
row["CurrencyFormat"] = ((TextBox)grow.Cells[0].FindControl("txtCurrencyFormat")).Text;
row["ConversionRate"] = ((TextBox)grow.Cells[1].FindControl("txtConversionRatenum")).Text;
dt.Rows.Add(row);


}

//add new extra row
row = dt.NewRow();
row["CurrencyFormat"] = string.Empty;
row["ConversionRate"] = string.Empty;
dt.Rows.Add(row);


gridviewConversionRate.DataSource = dt;
gridviewConversionRate.DataBind();
ViewState["CurrentTable"] = dt;

}



protected void btnNew_Click(object sender, EventArgs e)
{

addNewRowToGrid();

}
Left by Niran on May 04, 2012 6:58 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
what is "CurrentTable", is it the table from database or just dummy for gridview?
Left by AbdulKhaleq on May 15, 2012 7:50 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
it is really wonderful help only thing u need to add the onclick method for the button so that event will be fired. Wonderful sharing...
Left by sadhana on Jun 26, 2012 2:46 AM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
I need to calculate the sum of column1 data and column2 data and display in column3 and display the grand total of column3 in footer label its urgent can u say me how mail me link
Left by Nishok on Oct 03, 2012 6:20 PM

# re: Adding Dynamic Rows in GridView with TextBoxes
Requesting Gravatar...
Excellent work,after entering the values into the text boxes how can i store the values to the database on single button click.? could u please give me quick reply
Thanks in Advance
Left by Kumar on Nov 05, 2012 4:52 PM

# adding dropdownlist / textbox based on condition to a gridview
Requesting Gravatar...
HI
can you help me in creating a gridview adding dropdownlist / textbox based on condition to a gridview.

Thanks in advance for any idea.
Left by vamsi krishna on Dec 30, 2012 10:22 AM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net | Join free