Saturday, May 16, 2009

Update Database with DataGridView+Update Command

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;

namespace Trans829115.PL
{
public partial class frmUpdateDBViaDataGridView : Form
{
private OracleConnection connection;
private OracleCommand command;
private OracleDataAdapter adapter;
private OracleCommandBuilder builder;
private DataSet ds;
private DataTable userTable;
private bool mAllowInsert;
private bool mAllowDelete;
private bool IsDirty = false;


public frmUpdateDBViaDataGridView()
{
InitializeComponent();

// In Form control definition

connection = new OracleConnection(BLL.QuerySet.ConString);
command = new OracleCommand(BLL.QuerySet.UpFTP, connection);
adapter = new OracleDataAdapter(command);
builder = new OracleCommandBuilder(adapter);
ds = new DataSet();
adapter.Fill(ds);
userTable = ds.Tables[0];

userDataGridView.AllowUserToAddRows = true;
userDataGridView.AllowUserToDeleteRows = true;
btnDelete.Enabled = true;

}

private void btnDelete_Click(object sender, EventArgs e)
{
Delete();
}

private void frmUpdateDBViaDataGridView_Load(object sender, EventArgs e)
{
userDataGridView.DataSource = userTable.DefaultView;
lblRowCount.Text = "Number of records: " + userTable.Rows.Count.ToString();
userDataGridView.AllowUserToResizeColumns = true;
if (userTable.Rows.Count == 0)
{
btnDelete.Enabled = false;
btnUpdate.Enabled = false;
}

}

private void Delete()
{
if (MessageBox.Show("Do you really want to delete the selected record(s)?",
"Delete records", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false)
== DialogResult.Yes)
{
try
{

int cnt = userDataGridView.SelectedRows.Count;
for (int i = 0; i < cnt; i++)
{
if (this.userDataGridView.SelectedRows.Count > 0 &&
this.userDataGridView.SelectedRows[0].Index !=
this.userDataGridView.Rows.Count - 1)
{
this.userDataGridView.Rows.RemoveAt(
this.userDataGridView.SelectedRows[0].Index);
}
}

if(ds.HasChanges())
{
DataTable changes = ds.Tables[0].GetChanges();

if (changes != null)


adapter.Update(changes);
ds.Tables[0].AcceptChanges();


}

}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{

}
}
if (userTable.Rows.Count == 0)
{
btnUpdate.Enabled = false;
if (mAllowDelete) btnDelete.Enabled = false;
}
IsDirty = true;
}

private void Update()
{
try
{
connection.Open();
adapter.UpdateCommand = builder.GetUpdateCommand();
adapter.Update(userTable);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
connection.Close();
}
}

private void btnUpdate_Click(object sender, EventArgs e)
{
Update();
}

private void frmUpdateDBViaDataGridView_FormClosing(object sender, FormClosingEventArgs e)
{
if (IsDirty)
if (MessageBox.Show("Do you want to save changes?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false) == DialogResult.Yes)
Update();

}

private void userDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (IsDirty)
if (MessageBox.Show("Do you want to save changes?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false) == DialogResult.Yes)
Update();

}

private void userDataGridView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete) Delete();
e.Handled = true; // Cancel
}



}
}

No comments:

Post a Comment

 
Locations of visitors to this page