Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to append data in datatable
    text
    copied!<p>I have two text boxes and a button.In the buttons click event I add the values captured in the text boxes as a new row in my grdiview. Now what I want is when my page loads again my gridview will show the existing data which I add in it and append new record without using database. </p> <p><strong>Here is my code:</strong></p> <pre><code>private void BindGrid(int rowcount) { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add("First Name", typeof(String)); dt.Columns.Add("Last Name", typeof(String)); if (ViewState["CurrentData"] != null) { for (int i = 0; i &lt; rowcount + 1; i++) { dt = (DataTable)ViewState["CurrentData"]; if (dt.Rows.Count &gt; 0) { dr = dt.NewRow(); dr[0] = dt.Rows[0][0].ToString(); } } dr = dt.NewRow(); dr[0] = TextBox1.Text; dr[1] = TextBox2.Text; dt.Rows.Add(dr); } else { dr = dt.NewRow(); dr[0] = TextBox1.Text; dr[1] = TextBox2.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; } </code></pre> <p><strong>Button click event:</strong></p> <pre><code>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; TextBox2.Text = string.Empty; TextBox1.Focus(); } </code></pre> <p>This is my ASPX page:</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;asp:TextBox ID="TextBox1" runat="server"/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;asp:TextBox ID="TextBox2" runat="server"/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" &gt; &lt;AlternatingRowStyle BackColor="White" /&gt; &lt;EditRowStyle BackColor="#2461BF" /&gt; &lt;FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt; &lt;HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt; &lt;PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /&gt; &lt;RowStyle BackColor="#EFF3FB" /&gt; &lt;SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /&gt; &lt;SortedAscendingCellStyle BackColor="#F5F7FB" /&gt; &lt;SortedAscendingHeaderStyle BackColor="#6D95E1" /&gt; &lt;SortedDescendingCellStyle BackColor="#E9EBEF" /&gt; &lt;SortedDescendingHeaderStyle BackColor="#4870BE" /&gt; &lt;/asp:GridView&gt; &amp;nbsp;&lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload