Vinz' Blog

"Code, Beer and Music ~ my way of being a programmer"
posts - 129, comments - 469, trackbacks - 0

My Links

News

Archives

Image Galleries

I'm a...

I'm at...

Adding Multiple Columns and Rows in GridView without using a Database

This article describes on how to add multiple columns and rows in GridView without using a database. Basically the GridView will be populated with data based on the values entered in each TextBoxes on Button Click and retain the GridView data on post back.
 
 
STEP 1:  
Add Three 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:TextBox ID="TextBox2" runat="server"/>
<asp:TextBox ID="TextBox3" 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)));
        dt.Columns.Add(new System.Data.DataColumn("TextBox2Column", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("TextBox3Column", 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;
            dr[1] = TextBox2.Text;
            dr[2] = TextBox3.Text;
            dt.Rows.Add(dr);
 
        }
        else
        {
            dr = dt.NewRow();
            dr[0] = TextBox1.Text;
            dr[1] = TextBox2.Text;
            dr[2] = TextBox3.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 = string.Empty;
 
        TextBox1.Focus();
}
 
STEP 4:
Compile and run the application.
 
 
So when you type anything on each TextBoxes on the page and hit the Button, then the GridView will be populated with the data based on the TextBox values entered and retain its values on postback.

Print | posted on Wednesday, May 07, 2008 6:19 PM |

Feedback

Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

Awesome...Thanks a lot.
5/28/2008 11:07 PM | Gaurav
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

This is a great solution to my problem. What if I needed to add a DataColumn at index [0] with a CheckBox and another DataColumn at index[4] with a TextBox. How could I achieve this?
6/19/2008 2:20 AM | WhoIsPrime
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

I am Trainee Programmer at Comm-It India pvt. Ltd at New Delhi.using your articles i am full sastify.a person who wrote this articles is great....
6/20/2008 9:29 PM | Mohammad Javed
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

excellent work. i did everything except taking viewstate which made my table to loss previous details.
Thank you very much
8/4/2008 12:57 AM | raghu kishore
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

it is very good
8/25/2008 9:58 PM | praveenjettigari
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

cool dude !! highly appreciated....



Wamiq Mustafa
TagStone Tech, Dubai
10/3/2008 4:33 AM | Wamiq Mustafa
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

can u give me sample in asp.net and vb.net use linq?
10/12/2008 9:41 PM | omel
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

Its good .. but i need to show the data table in the grid view by default with some 2 or 3 blank rows ...it is possible
11/20/2008 8:57 PM | Sivakumar
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

Good article.....GR888888888888888888888.........,
But what if i need to edit,cancle and update
3/31/2009 10:21 PM | Sandesh
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

What is this , it is not working ??
8/10/2009 5:58 AM | Prakash
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

@Prakash,

What do you mean by not working? What have you tried?
8/10/2009 1:14 PM | Vinz
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

testing identicons
8/13/2009 10:01 PM | Ian
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

Thanks a lot... Great article

Can we add radio button, drop down list and a button by adding 3 more columns?

If so, can i know the procedure..
8/28/2009 2:09 PM | naresh
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

@Naresh,

See if this helps:
http://geekswithblogs.net/dotNETvinz/archive/2009/08/12/updated-adding-dynamic-rows-in-asp.net-gridview-control-with-textboxes.aspx
8/30/2009 5:11 PM | Vinz
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

This Article is good, we need the Same in VB.Net also
9/18/2009 6:03 PM | p.anbu
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

@p.anbu,

you can use online tool to convert C# to VB.NET or vise versa, check this link:
http://geekswithblogs.net/dotNETvinz/archive/2009/04/14/c-and-vb.net-code-converter.aspx
9/27/2009 6:02 PM | Vinz
Gravatar

# re: Adding Multiple Columns and Rows in GridView without using a Database

This is very awesome, Thanks. it helps me a lot.
11/18/2009 10:32 AM | Alma Mercado
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: