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 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.
 
 
Technorati Tags: ,
Posted on Wednesday, May 7, 2008 6:19 PM ASP.NET , GridView | Back to top


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

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
Awesome...Thanks a lot.
Left by Gaurav on May 28, 2008 11:07 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
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?
Left by WhoIsPrime on Jun 19, 2008 2:20 AM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
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....
Left by Mohammad Javed on Jun 20, 2008 9:29 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
excellent work. i did everything except taking viewstate which made my table to loss previous details.
Thank you very much
Left by raghu kishore on Aug 04, 2008 12:57 AM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
it is very good
Left by praveenjettigari on Aug 25, 2008 9:58 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
cool dude !! highly appreciated....



Wamiq Mustafa
TagStone Tech, Dubai
Left by Wamiq Mustafa on Oct 03, 2008 4:33 AM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
can u give me sample in asp.net and vb.net use linq?
Left by omel on Oct 12, 2008 9:41 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
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
Left by Sivakumar on Nov 20, 2008 8:57 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
Good article.....GR888888888888888888888.........,
But what if i need to edit,cancle and update
Left by Sandesh on Mar 31, 2009 10:21 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
What is this , it is not working ??
Left by Prakash on Aug 10, 2009 5:58 AM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
testing identicons
Left by Ian on Aug 13, 2009 10:01 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
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..
Left by naresh on Aug 28, 2009 2:09 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
This Article is good, we need the Same in VB.Net also
Left by p.anbu on Sep 18, 2009 6:03 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
This is very awesome, Thanks. it helps me a lot.
Left by Alma Mercado on Nov 18, 2009 10:32 AM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
great article!thanks
Left by ridhika on Aug 03, 2010 6:26 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
I have the following in my data table and I want to display it differently as in the second example below in a datagrid. Could you please light me with some ideas please?

UserID Questions Answers

1 What is your dog’s name belle

1 Tell us why you should win. Because I need to win

2 What is your dog’s name Leigh

2 Tell us why you should win. I’ve never won before
I'd like the get the result displayed like this below in a grid view.

UserID Question: What is your dog’s name Question: Tell us why you should win.

1 Answer: belle Answer: Because I need to win

2 Answer: Leigh Answer: I’ve never won before
Left by Ray on Dec 21, 2010 9:19 AM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
thnx buddy worked for me
Left by bhavik on Dec 07, 2011 2:17 PM

# re:Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
Very nice It is usefull and Understanding in .net language
try to give Delete Also
Left by VinodReddy on Dec 27, 2011 6:38 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
thanks for your solution....this solution very good and easily understand every one
Left by amaresh on Feb 22, 2012 11:46 AM

# re: Add Multiple GridView Rows into table using stroedprocedure
Requesting Gravatar...
hi please help me
Left by srinu on Mar 12, 2012 2:30 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
good job
Left by bhanu on May 07, 2012 2:32 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
This is very helpful for me........thanks for your solution.
Left by Babulal sah(Katihar,Bihar,India) on Jul 16, 2012 2:23 PM

# re: Adding Multiple Columns and Rows in GridView without using a Database
Requesting Gravatar...
hi.. this is very helpful for me.

thank u so much sharing ur knowledge..
and also i need code for edit from gridvied row to textbox..

if u can means please send me..

thank you...
Left by brindha.ks on Oct 31, 2012 11:54 PM

comments powered by Disqus

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