Click here to Skip to main content
15,898,134 members
Articles / Programming Languages / C#

.NET like Access(V1)

Rate me:
Please Sign up or sign in to vote.
3.20/5 (10 votes)
28 Feb 20067 min read 57.7K   542   40  
Database library for rapid development.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace DNetLibrary
{
	/// <summary>
	/// Summary description for UC_DataWindow.
	/// </summary>
	public class UC_DataWindow : System.Windows.Forms.UserControl
	{
		protected   DataHelper dm;
		private System.Data.DataSet ds_Data;
		public System.Windows.Forms.ContextMenu menContextMenue;
		private System.Windows.Forms.MenuItem mRetrieve;
		private System.Windows.Forms.MenuItem mFirst;
		private System.Windows.Forms.MenuItem mNext;
		private System.Windows.Forms.MenuItem mPrev;
		private System.Windows.Forms.MenuItem mLast;
		private System.Windows.Forms.MenuItem mInsert;
		private System.Windows.Forms.MenuItem mUpdate;
		private System.Windows.Forms.MenuItem mDelete;
		private System.Windows.Forms.MenuItem mDuplicate;
		private System.Windows.Forms.MenuItem mCancel;
		private System.Windows.Forms.MenuItem mSep1;
		private System.Windows.Forms.MenuItem mSep2;
		private System.Windows.Forms.MenuItem mSep3;
		private string sort="";
		private string filter="";
		bool applyFilter=false;
		bool applySort=false;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;		
		public event EventHandler RowChanged;
		public UC_DataWindow()
		{
			InitializeComponent();
			dm=new DataHelper();
			InitData();
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			ds_Data = new System.Data.DataSet();
			menContextMenue = new System.Windows.Forms.ContextMenu();
			mRetrieve = new System.Windows.Forms.MenuItem();
			mSep1 = new System.Windows.Forms.MenuItem();
			mFirst = new System.Windows.Forms.MenuItem();
			mNext = new System.Windows.Forms.MenuItem();
			mPrev = new System.Windows.Forms.MenuItem();
			mLast = new System.Windows.Forms.MenuItem();
			mSep2 = new System.Windows.Forms.MenuItem();
			mInsert = new System.Windows.Forms.MenuItem();
			mUpdate = new System.Windows.Forms.MenuItem();
			mDelete = new System.Windows.Forms.MenuItem();
			mDuplicate = new System.Windows.Forms.MenuItem();
			mSep3 = new System.Windows.Forms.MenuItem();
			mCancel = new System.Windows.Forms.MenuItem();
			((System.ComponentModel.ISupportInitialize)(ds_Data)).BeginInit();
			// 
			// ds_Data
			// 
			ds_Data.DataSetName = "DS_Data";
			ds_Data.Locale = new System.Globalization.CultureInfo("en-US");
			// 
			// menContextMenue
			// 
			menContextMenue.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   mRetrieve,
																					   mSep1,
																					   mFirst,
																					   mNext,
																					   mPrev,
																					   mLast,
																					   mSep2,
																					   mInsert,
																					   mUpdate,
																					   mDelete,
																					   mDuplicate,
																					   mSep3,
																					   mCancel});
			// 
			// mRetrieve
			// 
			mRetrieve.Index = 0;
			mRetrieve.Text = "R&etrieve";
			mRetrieve.Click += new System.EventHandler(mRetrieve_Click);
			// 
			// mSep1
			// 
			mSep1.Index = 1;
			mSep1.Text = "-";
			// 
			// mFirst
			// 
			mFirst.Index = 2;
			mFirst.Text = "&First";
			mFirst.Click += new System.EventHandler(mFirst_Click);
			// 
			// mNext
			// 
			mNext.Index = 3;
			mNext.Text = "&Next";
			mNext.Click += new System.EventHandler(mNext_Click);
			// 
			// mPrev
			// 
			mPrev.Index = 4;
			mPrev.Text = "&Previous";
			mPrev.Click += new System.EventHandler(mPrev_Click);
			// 
			// mLast
			// 
			mLast.Index = 5;
			mLast.Text = "&Last";
			mLast.Click += new System.EventHandler(mLast_Click);
			// 
			// mSep2
			// 
			mSep2.Index = 6;
			mSep2.Text = "-";
			// 
			// mInsert
			// 
			mInsert.Index = 7;
			mInsert.Text = "&Insert";
			mInsert.Click += new System.EventHandler(mInsert_Click);
			// 
			// mUpdate
			// 
			mUpdate.Index = 8;
			mUpdate.Text = "&Update";
			// 
			// mDelete
			// 
			mDelete.Index = 9;
			mDelete.Text = "&Delete";
			// 
			// mDuplicate
			// 
			mDuplicate.Index = 10;
			mDuplicate.Text = "D&uplicate";
			// 
			// mSep3
			// 
			mSep3.Index = 11;
			mSep3.Text = "-";
			// 
			// mCancel
			// 
			mCancel.Index = 12;
			mCancel.Text = "&Cancel";
			// 
			// UC_DataWindow
			// 
			BackColor = System.Drawing.Color.Silver;
			ContextMenu = menContextMenue;
			Name = "UC_DataWindow";
			Size = new System.Drawing.Size(288, 224);
			Load += new System.EventHandler(UC_DataWindow_Load);
			((System.ComponentModel.ISupportInitialize)(ds_Data)).EndInit();

		}
		#endregion
		DataTable table=new  DataTable();
		public DataTable Table
		{
			get
			{
				return table;
			}
			set
			{
				table=value;
			}
		}
				
		private bool retrieveAtOpening;
		public bool RetrieveAtOpening
		{
			get
			{
				return retrieveAtOpening;
			}
			set
			{
				retrieveAtOpening=value;
			}
		}

		public int Retrieve()
		{
			
			int result=-1;						
			string whereCondition="";
			string sort="";		
			try
			{
				if(ApplyFilter)whereCondition+=Filter;
				if(ApplySort)sort=Sort;
				table.Clear();
				result=dm.Retreive(table,whereCondition,sort);
				if(RowChanged!=null)RowChanged("Retrieve",new Args(RowNo));
				RaiseRowChangeEvent(0);
			}
			catch(Exception ex){MessageBox.Show(UTIL.GetExceptionMessage(ex));}
			return result;
		}

		public void MoveFirst()
		{
			BindingContext[ds_Data,table.TableName].Position=0;
			RaiseRowChangeEvent(1);			
		}

		public void MoveLast()
		{
			BindingContext[ds_Data,table.TableName].Position=BindingContext[ds_Data,table.TableName].Count-1;
			RaiseRowChangeEvent(RowNo);			
		}

		public void MoveNext()
		{
			if(BindingContext[ds_Data,table.TableName].Position==BindingContext[ds_Data,table.TableName].Count-1)
			{
				BindingContext[ds_Data,table.TableName].Position=0;
				RaiseRowChangeEvent(RowNo);
			}
			BindingContext[ds_Data,table.TableName].Position++;
			RaiseRowChangeEvent(RowNo);
		}

		public void MovePrevious()
		{
			if(BindingContext[ds_Data,table.TableName].Position==0)
			{
				BindingContext[ds_Data,table.TableName].Position=BindingContext[ds_Data,table.TableName].Count-1;
				RaiseRowChangeEvent(RowNo);
			}
			BindingContext[ds_Data,table.TableName].Position--;
			RaiseRowChangeEvent(RowNo);
		}

		public int InitData()
		{
			if(retrieveAtOpening)
			{
				return Retrieve();
			}
			return 0;
		}

		private void menuItem1_Click(object sender, System.EventArgs e)
		{
			Retrieve();
		}

		private void menuItem3_Click(object sender, System.EventArgs e)
		{
			MoveFirst();
		}

		private void menuItem4_Click(object sender, System.EventArgs e)
		{
			MoveNext();
		}

		private void menuItem5_Click(object sender, System.EventArgs e)
		{
			MovePrevious();
		}

		private void menuItem6_Click(object sender, System.EventArgs e)
		{
			MoveLast();
		}

		public DataSet DS_Data
		{
			get
			{
				return ds_Data;
			}
			set
			{
				ds_Data=value;
			}
		}

		public int Count
		{
			get
			{
				return BindingContext[ds_Data,table.TableName].Count;
			}			
		}

		public int RowNo
		{
			get
			{
				return BindingContext[ds_Data,table.TableName].Position+1;			
			}
			
		}

		public DataRow DataRowObject
		{
			get
			{
				return ds_Data.Tables[table.TableName].Rows[BindingContext[ds_Data,table.TableName].Position];			
			}
		}
		
		private void RaiseRowChangeEvent(int rowNo)
		{
			if(RowChanged!=null)RowChanged("Navigate",new Args(rowNo));
		}

		private void UC_DataWindow_Load(object sender, System.EventArgs e)
		{
		
		}

		private void mRetrieve_Click(object sender, System.EventArgs e)
		{
			Retrieve();
		
		}

		private void mFirst_Click(object sender, System.EventArgs e)
		{
		
			MoveFirst();
		}

		private void mNext_Click(object sender, System.EventArgs e)
		{
		
			MoveNext();
		}

		private void mPrev_Click(object sender, System.EventArgs e)
		{
			MovePrevious();
		
		}

		private void mLast_Click(object sender, System.EventArgs e)
		{
			MoveLast();
		
		}

		private void mInsert_Click(object sender, System.EventArgs e)
		{
		
		}
		
		public string Tag1
		{
			get
			{
				return null;
			}
			set
			{
			}
		}

		public string Sort
		{
			get
			{
				return sort;
			}
			set
			{
				sort=value;
			}
		}

		public bool ApplySort
		{
			get
			{
				return applySort;
			}
			set
			{
				if(Sort==null||Sort.Trim()=="")
				{
					return;
				}
				applySort=value;
				if(applySort)Retrieve();
			}
		}

		public string Filter
		{
			get
			{
				return filter;
			}
			set
			{
				filter=value;
			}
		}

		public bool ApplyFilter
		{
			get
			{
				return applyFilter;
			}
			set
			{
				if(Filter==null||Filter.Trim()=="")
				{
					return;
				}
				applyFilter=value;
				if(applyFilter)Retrieve();
			}
		}
		public void RemoveFilterSort()
		{
			Filter="";
			ApplySort=false;
			Sort="";
			ApplyFilter=false;
		}
		/*class*/}
	
	/*name space*/}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) NSW Curriculum & Learning Innovation Centre
Australia Australia
I am a senior developer self taught,
the main study is electronics and communication engineering

I am working as senior programmer in center for learning and innovation
www.cli.nsw.edu.au

I develop Software since 1995 using Delphi with Microsoft SQL Server

before 2000, I didn’t like Microsoft tools and did many projects using all database tools and other programming tools specially Borland C++ Builder, Delphi, Power Builder
And I loved Oracle database for its stability until I was certified as Master in Database Administration from Oracle University.

I tried to work in web programming but I felt that Java is hard and slow in programming, specially I love productivity.

I began worked with .Net since 2001 , and at the same time Microsoft SQL Server 7 was very stable so I switched all my way to Microsoft Tech.
I really respect .Net Platform especially in web applications

I love database Applications too much
And built library with PowerBuilder it was very useful for me and other developers

I have a wide experience due to my work in different companies
But the best experience I like in wireless applications, and web applications.
The best Application I did in my life is Novartis Marketing System 1999 it takes 8 months developing with PowerBuilder and Borland C++, SQL Server
Performance was the key challenge in this Application.
The other 2 applications that I loved Multilingual Project in Scada company in Italy 2000 and SDP Mobile media content platform server for ChinaUnicom 2004
I hope that you enjoy any Article I post.
God bless you.

Comments and Discussions