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)


table

Design chamber

Step 4

Open your gridview_demo.aspx file, where we create our design.

Gridview_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <style type="text/css">
  7. .style1
  8. {
  9. width: 550px;
  10. }
  11. .style2
  12. {
  13. font-size: large;
  14. text-decoration: underline;
  15. color: #990000;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <form id="form1" runat="server">
  21. <div class="style2">
  22. <strong>Move Data of Textbox to Gridview</strong></div>
  23. <table style="width:100%;">
  24. <tr>
  25. <td>
  26. Name:</td>
  27. <td class="style1">
  28. <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
  29. </td>
  30. <td>
  31. </td>
  32. </tr>
  33. <tr>
  34. <td>
  35. City:</td>
  36. <td class="style1">
  37. <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
  38. </td>
  39. <td>
  40. </td>
  41. </tr>
  42. <tr>
  43. <td>
  44. </td>
  45. <td class="style1">
  46. <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Save to Gridview</asp:LinkButton>
  47. </td>
  48. <td>
  49. </td>
  50. </tr>
  51. </table>
  52. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
  53. DataKeyNames="id" BackColor="LightGoldenrodYellow" BorderColor="Tan"
  54. BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
  55. <AlternatingRowStyle BackColor="PaleGoldenrod" />
  56. <Columns>
  57. <asp:TemplateField HeaderText="Name">
  58. <EditItemTemplate>
  59. <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
  60. </EditItemTemplate>
  61. <ItemTemplate>
  62. <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
  63. </ItemTemplate>
  64. </asp:TemplateField>
  65. <asp:TemplateField HeaderText="City">
  66. <EditItemTemplate>
  67. <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
  68. </EditItemTemplate>
  69. <ItemTemplate>
  70. <asp:Label ID="Label2" runat="server" Text='<%# Bind("city") %>'></asp:Label>
  71. </ItemTemplate>
  72. </asp:TemplateField>
  73. </Columns>
  74. <FooterStyle BackColor="Tan" />
  75. <HeaderStyle BackColor="Tan" Font-Bold="True" />
  76. <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
  77. HorizontalAlign="Center" />
  78. <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
  79. <SortedAscendingCellStyle BackColor="#FAFAE7" />
  80. <SortedAscendingHeaderStyle BackColor="#DAC09E" />
  81. <SortedDescendingCellStyle BackColor="#E1DB9C" />
  82. <SortedDescendingHeaderStyle BackColor="#C2A47B" />
  83. </asp:GridView>
  84. </form>
  85. </body>
  86. </html>

Your design will look like the following:

design

Code chamber

Step 6

Open gridview_demo.aspx.cs and write some code so that our application works.

Gridview_demo.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. public partial class _Default : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (!Page.IsPostBack)
  14. {
  15. refreshdata();
  16. }
  17. }
  18. public void refreshdata()
  19. {
  20. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  21. SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
  22. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  23. DataTable dt = new DataTable();
  24. sda.Fill(dt);
  25. GridView1.DataSource = dt;
  26. GridView1.DataBind();
  27. }
  28. protected void LinkButton1_Click(object sender, EventArgs e)
  29. {
  30. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  31. SqlCommand cmd = new SqlCommand("sp_insert", con);
  32. cmd.CommandType = CommandType.StoredProcedure;
  33. cmd.Parameters.AddWithValue("name", TextBox3.Text);
  34. cmd.Parameters.AddWithValue("city", TextBox4.Text);
  35. con.Open();
  36. int i = cmd.ExecuteNonQuery();
  37. con.Close();
  38. refreshdata();
  39. }
  40. }
Output chamber

Output

Enter detail


move data

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