how to edit datagridview in window application while the database is access.
and the changes should be reflect in database also.
like i displayed some tabe from access database in datagridview. then i want to changes values in datagridview and the new values sud be update in database.
pls someone suggest.
i am looking for this code for a while and have tried almost every technique
thanks in advance
Loading

Rakesh Jha85 reputationPosted Oct 6, 2010, 7:40 PM EDT
here is example which will help you to work on access database in windows form with datagridview.
1. first add this namespace
using System.Data.OleDb;
2. now write following code for display record in datagridview
protected DataSet ObjDS = new DataSet();
protected String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\HJHMHome.mdb";
private void DischargeForm_Load(object sender, EventArgs e)
{ (OleDbConnection ObjCon = new OleDbConnection(sConnectionString))
{ using (OleDbDataAdapter ObjAdapter = new OleDbDataAdapter("SELECT * FROM Investigation Order by ColOrder", ObjCon))
{ "Investigation");
dgvInvestigation.DataSource = ObjDS.Tables[0];
ObjCon.Close();
}
}
}
3. Update DataGridView on btnUpdate click
if (ObjDS.HasChanges())
{
DataTable dtChanged = new DataTable();
String ObjInvestigation = String.Empty;
String ObjInvestData = String.Empty;
dtChanged = ObjDS.Tables[0].GetChanges();
foreach (DataRow dr in dtChanged.Rows)
{
for (int iColCntr = 1; iColCntr < 6; iColCntr++)
{
ObjInvestigation = String.Empty;
ObjInvestData = String.Empty;
if (dr["Col" + iColCntr + "Name", DataRowVersion.Current].ToString() != dr["Col" + iColCntr + "Name", DataRowVersion.Original].ToString())
ObjInvestigation = "UPDATE Investigation SET Col" + iColCntr + "Name = '" + dr["Col" + iColCntr + "Name", DataRowVersion.Current].ToString().Replace("'", "''") + "' WHERE Col" + iColCntr + "Name = '" + dr["Col" + iColCntr + "Name", DataRowVersion.Original].ToString().Replace("'", "''") + "' AND Col" + iColCntr + "Value = '" + dr["Col" + iColCntr + "Value", DataRowVersion.Original].ToString() + "'";
if (dr["Col" + iColCntr + "Blank", DataRowVersion.Current].ToString() != dr["Col" + iColCntr + "Blank", DataRowVersion.Original].ToString())
ObjInvestData = "INSERT INTO InvestData(IPNo, InvColID, InvData) SELECT '" + txtIPNo.Text.Replace("'", "''") + "','" + dr["Col" + iColCntr + "Value", DataRowVersion.Current].ToString() + "','" + dr["Col" + iColCntr + "Blank", DataRowVersion.Current].ToString() + "'";
if (!String.IsNullOrEmpty(ObjInvestigation))
if (!String.IsNullOrEmpty(ObjInvestData))
ObjInvestData = ObjInvestData + "; " + ObjInvestigation;
else
ObjInvestData = ObjInvestigation;
if (!String.IsNullOrEmpty(ObjInvestData))
{
using (OleDbCommand ObjCommand = new OleDbCommand(ObjInvestData, ObjCon))
{
ObjCommand.CommandTimeout = 99999;
ObjCommand.CommandType = CommandType.Text;
if (ObjCon.State == ConnectionState.Open)
ObjCon.Close();
ObjCon.Open();
if (Convert.ToInt32(ObjCommand.ExecuteScalar()) >= 0)
ObjDS.AcceptChanges();
ObjCon.Close();
}
}
}
}
}
Posted Oct 7, 2010, 11:14 AM EDT
or can u pls upload its working sample with database.
pls
my now my job depends on it.
thanks and waiting ur reply
Sam HobbsRank #107 · 20.5K reputationPosted Oct 7, 2010, 4:45 PM EDT
Rakesh Jha85 reputationPosted Oct 8, 2010, 4:30 AM EDT
Hi ram,
sorry for late response. i am attaching access database file
Posted Oct 8, 2010, 5:13 PM EDT
please explain some design part of gv tht code too.
it will be great helpful to me .
i guess i am quiet near to the result.
thanks.
Posted Oct 8, 2010, 5:14 PM EDT
i have just taken a datagridview and a single update button .thts it .
please explain some design part of gv tht code too.
it will be great helpful to me .
i guess i am quiet near to the result.
thanks.
Rakesh Jha85 reputationPosted Oct 9, 2010, 5:47 AM EDT
I am attaching the source file.
Posted Oct 19, 2010, 6:03 AM EDT
i tried a lot but couldnot get the thing working .
i just need a datagridview having edit and delete options and that sud affect the database , that too in window.
may i have your email pls . want to have some discussion regrading this . or a working demo , i can pay a little for this as this is very important for me .
thanks.
Sam HobbsRank #107 · 20.5K reputationPosted Oct 19, 2010, 11:07 AM EDT
Posted Oct 19, 2010, 11:27 AM EDT
and too i couldnt get the navigation menu. in tht
Suthish NairRank #92 · 22.8K reputationPosted Oct 19, 2010, 11:33 AM EDT
Sams link already saying how to connect to DataSource. Rakesh code shows how to do the operations with tables.
What problem your facing. Be specific which area you facing the issue.
Sam HobbsRank #107 · 20.5K reputationPosted Oct 19, 2010, 12:00 PM EDT
Suthish NairRank #92 · 22.8K reputationPosted Oct 24, 2010, 11:56 PM EDT
Posted Oct 25, 2010, 7:07 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Oct 25, 2010, 10:27 AM EDT