Infolink

 

Search This Blog

Oct 29, 2013

Gridview in disconnected mode

aspx.cs

using BusinessLayer;
using PropertyLayer;
using System.Data.SqlClient;

namespace Gv_Disconnected_NTier_
{
    public partial class Gv_DisconnectedMode : System.Web.UI.Page
    {
        DataTable d1 = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                GvBusinessLayer objBusinessLayer = new GvBusinessLayer();
                d1 = objBusinessLayer.GetStudentDetails("student_records");
                ViewState["d1"] = d1;
                bind();
            }
            else
            {
                 d1 = (DataTable)ViewState["d1"];
            }
        }

        protected void bind()
        {
            Gv.DataSource = d1;
            Gv.DataBind();
        }

        protected void Gv_RowEditing(object sender, GridViewEditEventArgs e)
        {
             Gv.EditIndex = e.NewEditIndex;
             bind();
        }

        protected void Gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
             Gv.EditIndex = -1;
             bind();
        }

        protected void Gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DataRow[] dr;
            dr = d1.Select("id=" + Gv.Rows[e.RowIndex].Cells[0].Text);
            dr[0][1] = ((TextBox)Gv.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            dr[0][2] = ((TextBox)Gv.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            GvBusinessLayer objStudentDetails = new GvBusinessLayer();
            objStudentDetails.updateStudentDetails("student_records",d1);
            Gv.EditIndex = -1;
            bind();
        }

        protected void Gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataRow[] dr;
            dr = d1.Select("id=" + Gv.Rows[e.RowIndex].Cells[0].Text);
            GvBusinessLayer objStudentDetails = new GvBusinessLayer();
            objStudentDetails.updateStudentDetails("student_records", d1);
            dr[0].Delete();
            Gv.EditIndex = -1;
            bind();

        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            int max = 0;

            DataRow dr;
            for (int i = 0; i < Gv.PageCount; i++)
            {
                Gv.PageIndex = i;
                bind();
                for (int j = 0; j < Gv.Rows.Count; j++)
                {
                    if (max < Convert.ToInt16(Gv.Rows[j].Cells[0].Text))
                    {
                        max = Convert.ToInt16(Gv.Rows[j].Cells[0].Text);
                    }
                }
            }
            dr=d1.Rows.Add();
            Gv.PageIndex = Gv.PageCount;
            dr[0] = max + 1;
            bind();
            Gv.EditIndex = Gv.Rows.Count - 1;
            bind();
        }

        protected void Gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            Gv.PageIndex = e.NewPageIndex;
            bind();
        }
    }
}


GvDataLayer.cs

public class GvDataLayer
    {

        public static readonly string _connection = string.Empty;
        static GvDataLayer()
        {
            _connection = WebConfigurationManager.AppSettings["Gv_Disconnected"].ToString();
        }

        public static DataTable Dataset(string strTableName)
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from " + strTableName, _connection);
            DataTable dtselect = new DataTable();
            da.Fill(dtselect);
            return dtselect;
        }
        public static void Dataset(string strTableName,DataTable dt2)
        {      
            SqlDataAdapter da = new SqlDataAdapter("select * from " + strTableName, _connection);
            SqlCommandBuilder cmdb = new SqlCommandBuilder(da);
            da.Update(dt2);
        }
       
    }

PropertyLayer.cs

public class GvPropertyLayer
    {
        private int _Id = 0;
        private string _Name = string.Empty;
        private int _Marks = 0;
        private string _FirstName = string.Empty;
        private string _LastName = string.Empty;

        public int Id
        {
            get { return _Id; }
            set { _Id = value; }
        }
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        public int Marks
        {
            get { return _Marks; }
            set { _Marks = value; }
        }
        public string FirstName
        {
            get { return _FirstName; }
            set { _FirstName = value; }
        }
        public string LastName
        {
            get { return _LastName; }
            set { _LastName = value; }
        }
    }

BusinessLayer.cs

public class GvBusinessLayer
    {
        public DataTable GetStudentDetails(string strTableName)
        {
            return DataAcessLayer.GvDataLayer.Dataset(strTableName);
        }
        public void updateStudentDetails(string strTableName, DataTable dt)
        {

            DataAcessLayer.GvDataLayer.Dataset(strTableName,dt);

        }
    }

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...