0

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.

Here is my 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 < 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;
        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;
}

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;
    TextBox2.Text = string.Empty;
    TextBox1.Focus();
}

This is my ASPX page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs"   Inherits="Default2" %>

<!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>
     </head>
    <body>
        <form id="form1" runat="server">
       <div>

        <asp:TextBox ID="TextBox1" runat="server"/>&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox2" runat="server"/>&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:GridView          ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None" > <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
         <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
         <SortedDescendingCellStyle BackColor="#E9EBEF" />
         <SortedDescendingHeaderStyle BackColor="#4870BE" />
       </asp:GridView>

        &nbsp;</div>
        </form>
    </body>
  </html>
Marc
  • 3,905
  • 4
  • 21
  • 37
amitesh
  • 766
  • 5
  • 17
  • 39
  • create a new row of dt and put values in each column and add it to the dt and add back to viewstate – शेखर Feb 07 '13 at 08:53
  • @krshekhar can u explain it through code – amitesh Feb 07 '13 at 08:54
  • here is a link http://stackoverflow.com/questions/4731622/how-to-insert-a-new-row-into-datatable-in-c – शेखर Feb 07 '13 at 08:55
  • @amitesh do you mean you want to be able to navigate to other pages and later come back to **this** specific page and see the grid view populated with values which were captured earlier during the session? – Denys Wessels Feb 07 '13 at 09:14
  • see i add some data in the grid view using add button now when i open my page again and click on display button i can view those entered data that what i want – amitesh Feb 07 '13 at 10:07
  • @Deni what i want is to view my gridview when i click on display button and when i click on add button it will append that data in my grid view without any help of database and i want to use it with datatable and grid-view – amitesh Feb 07 '13 at 10:22
  • Could you please post your ASPX markup? – Denys Wessels Feb 07 '13 at 10:24
  • or we can say when i click on add button my data will be added in gridview and when i come again to my page it will show me details in gridview – amitesh Feb 07 '13 at 10:31
  • now i have one more ques guys i guess u are interested in it i have a submit button on same page when i click on that button a new gridview will appear with ol the data in the previous gridview i know the procedure but i am lack of coding part here – amitesh Feb 07 '13 at 10:53

1 Answers1

2

Here is a code which should work for you

private void BindGrid()
{
    GridView1.DataSource = GetDataTable();
    GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
    DataRow dr;
    DataTable dt = GetDataTable();
    dr = dt.NewRow();
    dr[0] = TextBox1.Text;
    dr[1] = TextBox2.Text;
    dt.Rows.Add(dr);
    ViewState["CurrentData"] = dt;
    TextBox1.Text = string.Empty;
    TextBox2.Text = string.Empty;
    TextBox1.Focus();
    //**updated**
    BindGrid();
}

protected DataTable GetDataTable()
{
    DataTable dt;
    if (ViewState["CurrentData"] != null)
    {
        dt = (DataTable)ViewState["CurrentData"];
    }
    else
    {
        dt = new DataTable();
        dt.Columns.Add("First Name", typeof(String));
        dt.Columns.Add("Last Name", typeof(String));
        //**Update**/
        ViewState["CurrentData"] = dt;
    }
    return dt;
}

and call the function BindGrid on page load.

Edit-1

if you want it after visiting another page then you should use session rather than viewstate
Change the functions accordingly

protected DataTable GetDataTable()
{
    DataTable dt;
    if (Session["CurrentData"] != null)
    {
        dt = (DataTable)Session["CurrentData"];
    }
    else
    {
        dt = new DataTable();
        dt.Columns.Add("First Name", typeof(String));
        dt.Columns.Add("Last Name", typeof(String));
        Session["CurrentData"] = dt;
    }
    return dt;
}
Marc
  • 3,905
  • 4
  • 21
  • 37
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • its not working when i click on add button i wont be able to view my gridview – amitesh Feb 07 '13 at 10:20
  • @amitesh call `BindGrid()` at the end of the button click function I missed that. I am updating my post accordingly – शेखर Feb 07 '13 at 10:49
  • can u tell me how cna we show data of one grid come from datatable into another – amitesh Feb 07 '13 at 12:37
  • 1
    @amitesh you can use the same `data-source` for both you `grid`. – शेखर Feb 07 '13 at 12:41
  • thanks @krshekher and i already done that but thanks for your answer now i have one more task to select and delete row from my first gridview i m doing that i will thank u if u help me on that – amitesh Feb 07 '13 at 13:03