Monday, 15 January 2018

Add TextBox value to GridView Without using Database

GridView :

Dear All,
This Demo shows you how to bind TextBox value to Grid without using Database
Eg.
Ø  Suppose that u have 3 TextBox and one Button Control and Grid
Ø  TextBox1 for FirstName, TextBox2 for LastName and TextBox3 for Id
Ø   Once u click on button the data get added to Grid Row
Ø  Then on next button click second new row get added to row
Ø  And so on…..
C# Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class TxtBoxDemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("First Name");
            DataColumn dc1 = new DataColumn("Last Name");
            DataColumn dc2 = new DataColumn("Id");

            dt.Columns.Add(dc);
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            ds.Tables.Add(dt);
            Session["data"] = ds;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = (DataSet)Session["data"];
        DataRow dr = ds.Tables[0].NewRow();
        dr[0] = txtFirstName.Text.Trim();
        dr[1] = txtLastName.Text.Trim();
        dr[2] = txtId.Text.Trim();
        ds.Tables[0].Rows.Add(dr);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}

4 comments:

  1. Dude,

    Thank you so much... :)

    ReplyDelete
  2. how can i edit,update this values in gridview

    ReplyDelete
  3. Hi Guys ...
    follow below link for more post..
    http://asp.velocitysolution.com/Home/Index.aspx

    ReplyDelete