Geeks With Blogs

News

Microsoft MVP


DZone MVB


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"

Well its seems that there is a little bug with my previous article about “ Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes “. The problem is that whenever you change the value of the previous data in the TextBox the updated values will not reflect on postbacks. So I have modified a bit of my codes at AddNewRowToGrid() and SetPreviousData() methods to fix the issue.

 

Here are the code blocks below for the updates:

 

    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;

 

                    dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;

                    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;

                    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;

 

                    rowIndex++;

                }

                dtCurrentTable.Rows.Add(drCurrentRow);

                ViewState["CurrentTable"] = dtCurrentTable;

 

                Gridview1.DataSource = dtCurrentTable;

                Gridview1.DataBind();

            }

        }

        else

        {

            Response.Write("ViewState is null");

        }

 

        //Set Previous Data on Postbacks

        SetPreviousData();

    }

 

 

    private void SetPreviousData()

    {

        int rowIndex = 0;

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

        {

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

            if (dt.Rows.Count > 0)

            {

                for (int i = 0; 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++;

                }

            }

        }

    }

 

That’s it! Let me know if you find any bugs.

 

Technorati Tags: ,,
Posted on Wednesday, August 12, 2009 5:08 PM ASP.NET , C# , GridView | Back to top


Comments on this post: UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes

# re: UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
Requesting Gravatar...
thanx fro the correction.it helped.
Left by Ashutosh (caGloo) on Jul 23, 2010 3:34 PM

# re: UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
Requesting Gravatar...
thx a lot,it works!
Left by ye on Nov 05, 2010 9:12 PM

# re: UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
Requesting Gravatar...
Hi Vin, great article. Just want to clarify with you, I have successfully added your code into my site, however when "Add New Row" button is clicked, is it normal behavior for the whole page to refresh? Is there a way to add it without having the page to "flicker"? Thanks!
Left by systemX on Apr 06, 2011 2:02 PM

# re: UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
Requesting Gravatar...
@systemX,

That's a normal behavior. Since we are on the context of HTTP, when you click on a button with type submit the whole page will be posted back to server for process and so it flickers the page. You can try using the UpdatePanel control and put the controls within it or you may implement it using cliient side scriptinng with JavaScript/jQuery.
Left by Vinz on Apr 06, 2011 2:13 PM

# re: UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
Requesting Gravatar...
Hi Vinz, thank you for your input. Just one more question regarding the gridview being used here. Is it possible to bind data into the gridview from your code? As I understand, the fields being used in the gridview is a TemplateField and not a BoundField, therefore I am not able to populate any data into it.

Here is what I am trying to do with my search form:
1. User keys in a search query (string)
2. If not found, your gridview code is presented to the user for the user to add rows and key in whatever data he wants and saves it.
3. However, if there is an existing record, then the same gridview code (yours) will be re-used and the data of that query will be populated into the gridview.

Sorry for posting an off-topic question, thanks for your time.
Left by systemX on Apr 07, 2011 3:20 PM

# re: UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
Requesting Gravatar...
@systemX,

You can still use TemplateField and populate it with your data from database. Take a look at this post:
http://geekswithblogs.net/dotNETvinz/archive/2009/06/09/adding-rows-in-gridview.aspx
Left by Vinz on Apr 07, 2011 8:08 PM

# re: UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
Requesting Gravatar...
Hi Vinz, is it possible to populate a drop down list in a gridview first, and then bind data to it? I have tried, and it does not work.
Left by systemX on Apr 13, 2011 3:46 PM

# re: UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
Requesting Gravatar...
Vinz, to be more specific, I am grabbing data from the database and putting it into the drop down list in the gridview, and after that I would like to fill up the gridview with the rest of the data as I am getting an error saying that the selectedvalue is not present in the drop down list when i bind the data first.
Left by systemX on Apr 13, 2011 3:47 PM

# re: UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
Requesting Gravatar...
Can you please provide the code for adding a new row when the gridview is bound with an sql datasource....would really appreciate it...
thanks
Left by Rahul on May 19, 2011 7:04 PM

# re: UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
Requesting Gravatar...
Hello Sir,

Pls suggest some code to add more then 1 row in grid.
Like if i enter 3 in text box and click AddNewRow button the 3 rows are added at time.
Left by Anuj on Aug 08, 2011 2:59 PM

# re: UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
Requesting Gravatar...
Vinz
Thanks for posting the solution. I have been looking to solve this problem for so long time.
Left by Anitha on Jul 20, 2012 12:07 AM

comments powered by Disqus

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