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"
This article describes on how to add rows in GridView without using a database. Basically the GridView will be populated with data based on the values entered in the TextBox on Button Click and retain the GridView data on post back.
 
 
STEP 1: 
Add One TextBox, One Button and One GridView control the web form. The ASPX mark-up should look like these below
 
<asp:TextBox ID="TextBox1" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" >
</asp:GridView>
 
 
STEP 2:
 
Create the method that will BIND the GridView based on the TextBox values and retain its values on post backs.
 
private void BindGrid(int rowcount)
    {
        DataTable dt = new DataTable();
        DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("TextBox1Column", typeof(String)));
 
        if (ViewState["CurrentData"] != null)
        {
            for (int i = 0; i < rowcount + 1; i++)
            {
                dt = (DataTable)ViewState["CurrentData"];
                if (dt.Rows.Count > 0)
                {
                    dr = dt.NewRow();
                    dr[0] = dt.Rows[0][0].ToString();
 
                }
            }
            dr = dt.NewRow();
            dr[0] = TextBox1.Text;
            dt.Rows.Add(dr);
 
        }
        else
        {
            dr = dt.NewRow();
            dr[0] = TextBox1.Text;
            dt.Rows.Add(dr);
 
        }
 
        // If ViewState has a data then use the value as the DataSource
        if (ViewState["CurrentData"] != null)
        {
            GridView1.DataSource = (DataTable)ViewState["CurrentData"];
            GridView1.DataBind();
        }
        else
        {
        // Bind GridView with the initial data assocaited in the DataTable
            GridView1.DataSource = dt;
            GridView1.DataBind();
 
        }
        // Store the DataTable in ViewState to retain the values
        ViewState["CurrentData"] = dt;
 
    }
 
 
Note that we store the DataTable in ViewState to retain the data values associated within the DataTable and use that as our GridView DataSource when it post back to the server.
 
STEP 3:
 
Binding the GridView on Button_Click event.
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        // Check if the ViewState has a data assoiciated within it. If
        if (ViewState["CurrentData"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentData"];
            int count = dt.Rows.Count;
            BindGrid(count);
        }
        else
        {
            BindGrid(1);
        }
        TextBox1.Text = “”;
        TextBox1.Focus();
    }
 
STEP 4:
Compile and run the application. The out put should look like below
 
 
So when you type anything on the TextBox and hit the Button, then the GridView will be populated with the data you entered in the TextBox and retain its values on postback.
 
Happy Coding! :)
 
Technorati Tags: ,
Posted on Wednesday, May 7, 2008 6:22 PM ASP.NET , GridView | Back to top


Comments on this post: Adding Rows in GridView without using a Database

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
if (!IsPostBack)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("ID");
DataColumn dc1 = new DataColumn("Product");
DataColumn dc2 = new DataColumn("Qnty");

dt.Columns.Add(dc);
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
ds.Tables.Add(dt);
Session["data"] = ds;
//GridView1.DataSource = ds;
//GridView1.DataBind();
}


protected void btn_Click(object sender, EventArgs e)
{





DataSet ds = (DataSet)Session["data"];
DataRow dr = ds.Tables[0].NewRow();
dr[0] = tb1.Text;
dr[1] = tb2.Text;
dr[2] = tb3.Text;
ds.Tables[0].Rows.Add(dr);
GridView1.DataSource = ds;
GridView1.DataBind();

}
Left by sravan on Nov 09, 2008 11:21 PM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
This coding is really helpful.
Thank u.
Left by palaniyammal on Nov 30, 2008 8:22 PM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
Thanks for your help
Left by coder on Mar 07, 2009 10:34 AM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
thanks vinz! really helpful.. i used your coding in my project.. but i cant figure out how to do paging an sorting from there
Left by hondaMugen on Nov 05, 2009 1:49 PM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
Thanks.It's simple and very useful.
Left by Star on Sep 17, 2010 7:37 PM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
Thanks good article ;)
Left by İhlas Temizlik Robotu on May 03, 2011 7:58 PM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
-------call ------------
protected void Page_Load(object sender, EventArgs e)
{
BindGrid(8);
}
Left by sureka on Jun 22, 2011 6:30 PM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
--------small adding to code els its shown error----------
protected void Page_Load(object sender, EventArgs e)
{
BindGrid(8);
}
Left by sureka on Jun 22, 2011 6:32 PM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
thank u it is very worst process
Left by ramana on Nov 22, 2011 1:04 PM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
If i want to put the last record in the first row?. I need help.
Regards
Left by luis on Nov 25, 2011 2:57 PM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
Thank u
this is very helpful..
Left by ramesh on Dec 27, 2011 7:01 PM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
Thanks ..This saved my time..
Left by Harpreet Singh on Feb 02, 2012 4:31 PM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
Thanks, Its very useful to me.
Left by guna on Feb 13, 2012 2:33 PM

# re: Adding Rows in GridView without using a Database
Requesting Gravatar...
Many Many Thanks....It's Very Very Use full..But How to Delete that Session Data not all other session data, if we clock close.

Actually i am using Different Tab in a Form.

If Again i click that tab then data again appears.
If i Clear the Session the all Session like Login/...Destroys.

What i should Use for clearing a particular session...
Left by Sudhanshu Shekhar on Sep 21, 2012 6:24 PM

comments powered by Disqus

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