Full width home advertisement

Post Page Advertisement [Top]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    DataTable dt= new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ViewState["CompDetails"] == null)
        {

            dt.Columns.Add("id");

            dt.Columns.Add("name");


            dt.Columns.Add("age");


            dt.Columns.Add("mobile");

        }

        else
        {


            dt = (DataTable)ViewState["CompDetails"];

        }




        DataRow dr = dt.NewRow();

        dr["id"] = Convert.ToInt32(TextBox4.Text);

        dr["name"] = (TextBox1.Text);


        dr["age"] = TextBox2.Text;


        dr["mobile"] = TextBox3.Text;


        dt.Rows.Add(dr);


        ViewState["CompDetails"] = dt;


        GridView1.DataSource = dt;

        GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        dt = (DataTable)ViewState["CompDetails"];


        dt.Rows[e.RowIndex].Delete();

        GridView1.DataSource = dt;

        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        dt = (DataTable)ViewState["CompDetails"];
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        int Id = Convert.ToInt32(((TextBox)row.Cells[1].Controls[0]).Text);
        string name = ((TextBox)row.Cells[2].Controls[0]).Text;
        string age = ((TextBox)row.Cells[3].Controls[0]).Text;
        string mobile = ((TextBox)row.Cells[4].Controls[0]).Text;

        DataRow[] datarow = dt.Select("ID='" + Id + "'");
        dt.Rows[Id].BeginEdit();
        dt.Rows[Id]["name"] = name;
        dt.Rows[Id]["age"] = age;
        dt.Rows[Id]["mobile"] = mobile;
        dt.Rows[Id].EndEdit();
        dt.AcceptChanges();
        GridView1.EditIndex = -1;
        GridView1.DataSource = dt;
        GridView1.DataBind();
     
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        dt = (DataTable)ViewState["CompDetails"];
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib