This article shows how to move textbox values to a GridView at run time using ASP.NET C#.
We are using two Textboxes, a button and a GridView, so when we fill the Textbox and hit the button the values are saved in the GridView.
Initial chamber
Step 1
Open your Visual Studio 2010 and create an empty website. Name it gridview_demo.
Step 2
In Solution Explorer you will see your empty website, add a web form and a SQL Server database as in the following.
For Web Form:
gridview_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it gridview_demo.aspx.
For SQL Server Database
gridview_demo (your empty website) then right-click then select Add New Item -> SQL Server Database. Add the database inside the App_Data_folder.
DATABASE CHAMBER
Step 3
Go to your database (Database.mdf) and create a table tbl_Data. Go to the database.mdf, then Table and Add New table. Design your table like the following:
Table tbl_data (don't forget to make ID as IS Identity -- True)

Design chamber
Step 4
Open your gridview_demo.aspx file, where we create our design.
Gridview_demo.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <style type="text/css">
- .style1
- {
- width: 550px;
- }
- .style2
- {
- font-size: large;
- text-decoration: underline;
- color: #990000;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div class="style2">
- <strong>Move Data of Textbox to Gridview</strong></div>
- <table style="width:100%;">
- <tr>
- <td>
- Name:</td>
- <td class="style1">
- <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- City:</td>
- <td class="style1">
- <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td class="style1">
- <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Save to Gridview</asp:LinkButton>
- </td>
- <td>
- </td>
- </tr>
- </table>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
- DataKeyNames="id" BackColor="LightGoldenrodYellow" BorderColor="Tan"
- BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
- <AlternatingRowStyle BackColor="PaleGoldenrod" />
- <Columns>
- <asp:TemplateField HeaderText="Name">
- <EditItemTemplate>
- <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="City">
- <EditItemTemplate>
- <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label2" runat="server" Text='<%# Bind("city") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- <FooterStyle BackColor="Tan" />
- <HeaderStyle BackColor="Tan" Font-Bold="True" />
- <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
- HorizontalAlign="Center" />
- <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
- <SortedAscendingCellStyle BackColor="#FAFAE7" />
- <SortedAscendingHeaderStyle BackColor="#DAC09E" />
- <SortedDescendingCellStyle BackColor="#E1DB9C" />
- <SortedDescendingHeaderStyle BackColor="#C2A47B" />
- </asp:GridView>
- </form>
- </body>
- </html>
Your design will look like the following:
Code chamber
Step 6
Open gridview_demo.aspx.cs and write some code so that our application works.
Gridview_demo.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- refreshdata();
- }
- }
- public void refreshdata()
- {
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- sda.Fill(dt);
- GridView1.DataSource = dt;
- GridView1.DataBind();
- }
- protected void LinkButton1_Click(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("sp_insert", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("name", TextBox3.Text);
- cmd.Parameters.AddWithValue("city", TextBox4.Text);
- con.Open();
- int i = cmd.ExecuteNonQuery();
- con.Close();
- refreshdata();
- }
- }


I hope you liked it. Thank you for reading, Have a good day.

dhoni kholiPosted Jan 7, 2020, 12:29 AM
How to delete One row on it; How to do that
Arul RPosted Oct 28, 2015, 5:34 AM
Good one !!!
Yashwanth MuthineniPosted Aug 14, 2015, 12:56 AM
Good one
Vipul MalhotraPosted Aug 13, 2015, 10:10 AM
nice article
Chervine BhiwooPosted Aug 12, 2015, 10:52 AM
Good work!
Gopi ChandPosted Aug 12, 2015, 7:26 AM
Great one
Rahul PrajapatPosted Aug 12, 2015, 3:11 AM
Nice share sir.
RakeshPosted Aug 11, 2015, 2:33 PM
Nice one
Shridhar SharmaPosted Aug 11, 2015, 11:35 AM
good one keep it up :)
Karthikeyan KPosted Aug 11, 2015, 8:11 AM
Good one sir...
Rajeesh MenothPosted Aug 11, 2015, 6:36 AM
Nice share
Pankaj Kumar ChoudharyPosted Aug 11, 2015, 6:33 AM
Nice Explain Sir..........
Khan Abrar AhmedPosted Aug 11, 2015, 6:29 AM
Nice artical
Sibeesh VenuPosted Aug 11, 2015, 6:15 AM
Nice Share