Hi Friends, The below code is a sample code for insert, update and delete using datagrid but i need to convert the entire code for datagridview.. Plsss help me..
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;
namespace UsingDataGridForBeginners
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class clsDataGridForm : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button btExit;
private System.Windows.Forms.Button btInsertnew;
private System.Windows.Forms.Button btUpdate;
private System.Windows.Forms.Button btDelete;
private System.Windows.Forms.Button btRefresh;
private System.Windows.Forms.Button btCopyToXml;
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
private UsingDataGridForBeginners.DataSet1 dataSet11;
private int iRowIndex=0;
private DataTable dtbl=new DataTable();
private DataTable tbl=new DataTable("MyContactsTable");
private System.Windows.Forms.Button btSearch;
private System.Windows.Forms.Button btFromXML;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public clsDataGridForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <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 Windows Form 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()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataSet11 = new UsingDataGridForBeginners.DataSet1();
this.btInsertnew = new System.Windows.Forms.Button();
this.btUpdate = new System.Windows.Forms.Button();
this.btDelete = new System.Windows.Forms.Button();
this.btFromXML = new System.Windows.Forms.Button();
this.btRefresh = new System.Windows.Forms.Button();
this.btCopyToXml = new System.Windows.Forms.Button();
this.btExit = new System.Windows.Forms.Button();
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
this.btSearch = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.AllowSorting = false;
this.dataGrid1.AlternatingBackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
this.dataGrid1.CaptionBackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(64)));
this.dataGrid1.CaptionText = "My Contacts Table";
this.dataGrid1.DataMember = "";
this.dataGrid1.DataSource = this.dataSet11.MyContactsTable;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.SelectionBackColor = System.Drawing.Color.Blue;
this.dataGrid1.Size = new System.Drawing.Size(656, 232);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("en-GB");
//
// btInsertnew
//
this.btInsertnew.BackColor = System.Drawing.SystemColors.Control;
this.btInsertnew.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btInsertnew.Location = new System.Drawing.Point(8, 248);
this.btInsertnew.Name = "btInsertnew";
this.btInsertnew.Size = new System.Drawing.Size(72, 24);
this.btInsertnew.TabIndex = 1;
this.btInsertnew.Text = "Insert";
this.btInsertnew.Click += new System.EventHandler(this.fnAllButtonsClicks);
//
// btUpdate
//
this.btUpdate.BackColor = System.Drawing.SystemColors.Control;
this.btUpdate.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btUpdate.Location = new System.Drawing.Point(88, 248);
this.btUpdate.Name = "btUpdate";
this.btUpdate.Size = new System.Drawing.Size(80, 24);
this.btUpdate.TabIndex = 1;
this.btUpdate.Text = "Save/Update";
this.btUpdate.Click += new System.EventHandler(this.fnAllButtonsClicks);
//
// btDelete
//
this.btDelete.BackColor = System.Drawing.SystemColors.Control;
this.btDelete.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btDelete.Location = new System.Drawing.Point(176, 248);
this.btDelete.Name = "btDelete";
this.btDelete.Size = new System.Drawing.Size(72, 24);
this.btDelete.TabIndex = 1;
this.btDelete.Text = "Delete";
this.btDelete.Click += new System.EventHandler(this.fnAllButtonsClicks);
//
// btFromXML
//
this.btFromXML.BackColor = System.Drawing.SystemColors.Control;
this.btFromXML.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btFromXML.Location = new System.Drawing.Point(256, 248);
this.btFromXML.Name = "btFromXML";
this.btFromXML.Size = new System.Drawing.Size(80, 24);
this.btFromXML.TabIndex = 1;
this.btFromXML.Text = "ReadFromXML";
this.btFromXML.Click += new System.EventHandler(this.fnAllButtonsClicks);
//
// btRefresh
//
this.btRefresh.BackColor = System.Drawing.SystemColors.Control;
this.btRefresh.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btRefresh.Location = new System.Drawing.Point(344, 248);
this.btRefresh.Name = "btRefresh";
this.btRefresh.Size = new System.Drawing.Size(75, 24);
this.btRefresh.TabIndex = 1;
this.btRefresh.Text = "Refresh";
this.btRefresh.Click += new System.EventHandler(this.fnAllButtonsClicks);
//
// btCopyToXml
//
this.btCopyToXml.BackColor = System.Drawing.SystemColors.Control;
this.btCopyToXml.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btCopyToXml.Location = new System.Drawing.Point(432, 248);
this.btCopyToXml.Name = "btCopyToXml";
this.btCopyToXml.Size = new System.Drawing.Size(80, 24);
this.btCopyToXml.TabIndex = 1;
this.btCopyToXml.Text = "CopyToXML";
this.btCopyToXml.Click += new System.EventHandler(this.fnAllButtonsClicks);
//
// btExit
//
this.btExit.BackColor = System.Drawing.Color.Red;
this.btExit.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btExit.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btExit.Location = new System.Drawing.Point(616, 248);
this.btExit.Name = "btExit";
this.btExit.Size = new System.Drawing.Size(32, 24);
this.btExit.TabIndex = 1;
this.btExit.Text = "Exit";
this.btExit.Click += new System.EventHandler(this.fnAllButtonsClicks);
//
// oleDbSelectCommand1
//
this.oleDbSelectCommand1.CommandText = "SELECT Address, City, Country, Email, FirstName, LastName, Message, Phone FROM My" +
"ContactsTable";
this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\\\..\\\\MyContactsDB.mdb";
//
// oleDbInsertCommand1
//
this.oleDbInsertCommand1.CommandText = "INSERT INTO MyContactsTable(Address, City, Country, Email, FirstName, LastName, M" +
"essage, Phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Address", System.Data.OleDb.OleDbType.VarWChar, 50, "Address"));
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("City", System.Data.OleDb.OleDbType.VarWChar, 50, "City"));
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Country", System.Data.OleDb.OleDbType.VarWChar, 50, "Country"));
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Email", System.Data.OleDb.OleDbType.VarWChar, 50, "Email"));
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("FirstName", System.Data.OleDb.OleDbType.VarWChar, 50, "FirstName"));
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("LastName", System.Data.OleDb.OleDbType.VarWChar, 50, "LastName"));
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Message", System.Data.OleDb.OleDbType.VarWChar, 50, "Message"));
this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Phone", System.Data.OleDb.OleDbType.VarWChar, 50, "Phone"));
//
// oleDbUpdateCommand1
//
this.oleDbUpdateCommand1.CommandText = @"UPDATE MyContactsTable SET Address = ?, City = ?, Country = ?, Email = ?, FirstName = ?, LastName = ?, Message = ?, Phone = ? WHERE (LastName = ?) AND (Address = ? OR ? IS NULL AND Address IS NULL) AND (City = ? OR ? IS NULL AND City IS NULL) AND (Country = ? OR ? IS NULL AND Country IS NULL) AND (Email = ? OR ? IS NULL AND Email IS NULL) AND (FirstName = ? OR ? IS NULL AND FirstName IS NULL) AND (Message = ? OR ? IS NULL AND Message IS NULL) AND (Phone = ? OR ? IS NULL AND Phone IS NULL)";
this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Address", System.Data.OleDb.OleDbType.VarWChar, 50, "Address"));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("City", System.Data.OleDb.OleDbType.VarWChar, 50, "City"));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Country", System.Data.OleDb.OleDbType.VarWChar, 50, "Country"));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Email", System.Data.OleDb.OleDbType.VarWChar, 50, "Email"));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("FirstName", System.Data.OleDb.OleDbType.VarWChar, 50, "FirstName"));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("LastName", System.Data.OleDb.OleDbType.VarWChar, 50, "LastName"));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Message", System.Data.OleDb.OleDbType.VarWChar, 50, "Message"));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Phone", System.Data.OleDb.OleDbType.VarWChar, 50, "Phone"));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_LastName", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "LastName", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Address", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Address1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_City", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_City1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Country", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Country1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Email", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Email", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Email1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Email", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_FirstName", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FirstName", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_FirstName1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FirstName", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Message", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Message", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Message1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Message", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Phone", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Phone1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null));
//
// oleDbDeleteCommand1
//
this.oleDbDeleteCommand1.CommandText = @"DELETE FROM MyContactsTable WHERE (LastName = ?) AND (Address = ? OR ? IS NULL AND Address IS NULL) AND (City = ? OR ? IS NULL AND City IS NULL) AND (Country = ? OR ? IS NULL AND Country IS NULL) AND (Email = ? OR ? IS NULL AND Email IS NULL) AND (FirstName = ? OR ? IS NULL AND FirstName IS NULL) AND (Message = ? OR ? IS NULL AND Message IS NULL) AND (Phone = ? OR ? IS NULL AND Phone IS NULL)";
this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_LastName", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "LastName", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Address", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Address1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_City", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_City1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Country", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Country1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Email", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Email", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Email1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Email", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_FirstName", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FirstName", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_FirstName1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "FirstName", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Message", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Message", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Message1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Message", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Phone", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null));
this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Phone1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null));
//
// oleDbDataAdapter1
//
this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "MyContactsTable", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("Address", "Address"),
new System.Data.Common.DataColumnMapping("City", "City"),
new System.Data.Common.DataColumnMapping("Country", "Country"),
new System.Data.Common.DataColumnMapping("Email", "Email"),
new System.Data.Common.DataColumnMapping("FirstName", "FirstName"),
new System.Data.Common.DataColumnMapping("LastName", "LastName"),
new System.Data.Common.DataColumnMapping("Message", "Message"),
new System.Data.Common.DataColumnMapping("Phone", "Phone")})});
this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
//
// btSearch
//
this.btSearch.BackColor = System.Drawing.SystemColors.Control;
this.btSearch.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btSearch.Location = new System.Drawing.Point(520, 248);
this.btSearch.Name = "btSearch";
this.btSearch.Size = new System.Drawing.Size(88, 24);
this.btSearch.TabIndex = 1;
this.btSearch.Text = "WriteToTextFile";
this.btSearch.Click += new System.EventHandler(this.fnAllButtonsClicks);
//
// clsDataGridForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.Olive;
this.ClientSize = new System.Drawing.Size(654, 289);
this.Controls.Add(this.btInsertnew);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.btUpdate);
this.Controls.Add(this.btDelete);
this.Controls.Add(this.btFromXML);
this.Controls.Add(this.btRefresh);
this.Controls.Add(this.btCopyToXml);
this.Controls.Add(this.btExit);
this.Controls.Add(this.btSearch);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.Name = "clsDataGridForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "DataGrid Form";
this.Load += new System.EventHandler(this.clsDataGridForm_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new clsDataGridForm());
}
//--------------------------------------------------------------------
// when the application first time runs
private void clsDataGridForm_Load(object sender, System.EventArgs e)
{
try
{
//connection was ok.display data in Datagrid
fnRefresh();
}
catch(Exception ex)
{
MessageBox.Show("Connection failed..."+ex.Message);
Application.Exit(); //end the program
}
}
//--------------------------------------------------------------------------
// All the buttons-clicks captured here
private void fnAllButtonsClicks(object sender, System.EventArgs e)
{
Button bt=(Button)sender; //get which button clicked
switch (bt.Text) //button-text
{
case "Insert" : fnInsertNew();
break;
case "Save/Update"
: fnSaveUpdate();
break;
case "Delete" : fnDelete();
break;
case "ReadFromXML"
: fnDataReadingFromXMLFile("C:\\MyXMLdata.xml");
break;
case "Refresh" : fnRefresh();
fnEnableDisableAllButtons(true);
break;
case "CopyToXML": fnCopyToXMLandTextFile();
break;
case "WriteToTextFile"
: fnWriteToTextFile();
break;
case "Exit" : Application.Exit();
break;
}//switch
}
//--------------------------------------------------------------------------
//
private void fnInsertNew()
{
//keep in mind the previous clicked row to unselect
int iPrevRowindex=this.iRowIndex;
MessageBox.Show("Enter the new record at the end of the DataGrid and click 'Save/Update'-button", "Stop");
this.btInsertnew.Enabled=false;
//get how many records in the table
this.iRowIndex=this.dataSet11.Tables["MyContactsTable"].Rows.Count;
//select the last row
this.dataGrid1.Select(this.iRowIndex);
//unselect the previous row
this.dataGrid1.UnSelect(iPrevRowindex);
}
//--------------------------------------------------------------------------
//Update or save if made any changes otherwise either no changes or error
private void fnSaveUpdate()
{
try
{
//put the modified DataSet into a new DataSet(myChangedDataset)
DataSet myChangedDataset= this.dataSet11.GetChanges();
if (myChangedDataset != null)
{
//get how many rows changed
int modifiedRows = this.oleDbDataAdapter1.Update(myChangedDataset);
MessageBox.Show("Database has been updated successfully: " + modifiedRows + " Modified row(s) ", "Success");
this.dataSet11.AcceptChanges(); //accept the all changes
fnRefresh();
}
else //no changes
{
MessageBox.Show("Nothing to save", "No changes");
}//if-else
}
catch(Exception ex)
{
//if something somehow went wrong
MessageBox.Show("An error occurred updating the database: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.dataSet11.RejectChanges(); //cancel the changes
}//try-catch
fnEnableDisableAllButtons(true);
}
//--------------------------------------------------------------------------
//
private void fnDelete()
{
//ask user if wanting to delete
DialogResult dr=MessageBox.Show("Are you sure you want to delete this row ? ", "Confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr ==DialogResult.Yes) //if
{
//user clicked the "Delete" button
DataTable tbl=new DataTable("MyContactsTable");
tbl=this.dataSet11.Tables[0];
int i=this.iRowIndex;//get the index of the row you clicked
tbl.Rows[i].Delete(); //delete the row
this.oleDbDataAdapter1.Update(tbl); //update the table
this.fnRefresh(); //refresh the table
}//if
}
//--------------------------------------------------------------------------
//
private void fnDataReadingFromXMLFile(string filename)
{
//check if the file exists
if ( File.Exists(filename))
{
//clear the DataSet contents
this.dataSet11.Clear();
MessageBox.Show("Data reading from "+filename+" -file");
this.dataSet11.ReadXml(filename);
} //if
else {
MessageBox.Show(filename+" does NOT exist; Please click first the button 'CopyToXML' ", "File Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
}//else
}
//---------------------------------------------------------------------------
// clear DataSet-contents, refresh the DataSet and display data in Datgrid
private void fnRefresh()
{
this.dataSet11.Clear();
this.oleDbDataAdapter1.Fill(this.dataSet11,"MyContactsTable");
this.dataGrid1.DataSource=this.dataSet11.Tables["MyContactsTable"].DefaultView;
}
//----------------------------------------------------------------------------
// it writes the current data of the DataSet to 3 files on C:\
//1.Text file: MyXMLdataText.txt
//2.XML file: XMLdata.xml
//3.schema file: MyXMLschema.xsd
private void fnCopyToXMLandTextFile()
{
if (this.dataSet11 == null)
{
return;
}
// Writes the current data for the DataSet to the specified files.
this.dataSet11.WriteXml("C:\\MyXMLText.txt");
this.dataSet11.WriteXml("C:\\MyXMLdata.xml", XmlWriteMode.WriteSchema);
this.dataSet11.WriteXmlSchema("C:\\MyXMLschema.xsd");
string s="The current data of the DataSet coppied on C:\\ as: \n";
s +="* Text file: MyXMLdataText.txt\n";
s +="* XML file: XMLdata.xml\n";
s +="* Schema file: MyXMLschema.xsd";
MessageBox.Show(s, "Copy to XML/Text/Schema file");
}
private void fnWriteToTextFile()
{
OleDbDataReader reader;
OleDbCommand cmd=new OleDbCommand();
this.oleDbConnection1.Open(); //open the connection
cmd.CommandText="SELECT Address, City, Country, Email, FirstName, LastName, Message, Phone FROM MyContactsTable";
cmd.Connection=this.oleDbConnection1;
reader=cmd.ExecuteReader();
//the "using" statement causes the close method to be called internally.
//if "using" not used, use "writer.Close()" in try-catch-finally explicitly
using (StreamWriter writer = new StreamWriter("C:\\MyTextFile.txt",false))
{ //false means: textfile is overwritten
try
{
while (reader.Read())
{
writer.Write(reader["LastName"]);
writer.Write("#");
writer.Write(reader["FirstName"]);
writer.Write("#");
writer.Write(reader["Address"]);
writer.Write("#");
writer.Write(reader["City"]);
writer.Write("#");
writer.Write(reader["Country"]);
writer.Write("#");
writer.Write(reader["Email"]);
writer.Write("#");
writer.Write(reader["Message"]);
writer.Write("#");
writer.Write(reader["Phone"]);
writer.WriteLine(); //next new line
}//while
}
catch (Exception excp)
{
MessageBox.Show(excp.Message);
}finally {
reader.Close(); //close the reader
}//try-catch
}//using
MessageBox.Show("Data of table writtten into C:\\MyTextFile.txt file","Writing completed");
}
//--------------------------------------------------------------------------
//get the row index on the DataGrid if clicked a row
private void dataGrid1_Click(object sender, System.EventArgs e)
{
this.iRowIndex =this.dataGrid1.CurrentRowIndex;
this.btInsertnew.Enabled=true;
fnShowCurrentRecord(this.iRowIndex);
}
//-------------------------------------------------------------------------
//shows the current record number
private void fnShowCurrentRecord(int pos)
{
this.dataGrid1.CaptionText=" Record: "+((pos)+1);
}
//-------------------------------------------------------------------------
//- This is the way to enable or disable ALL the buttons on the Form
//- if flag true all buttons enabled, if false buttons disabled
public void fnEnableDisableAllButtons(bool flag)
{
string str;
foreach(Control ctrl in this.Controls)
{
str = Convert .ToString(ctrl.GetType());
if(str == "System.Windows.Forms.Button")
{
ctrl.Enabled = flag;
}//if
}//foreach
}
}//class
}//namespace