Re: databinding problem
- From: "W.G. Ryan - MVP" <WilliamRyan@xxxxxxxxxxxxxxxx>
- Date: Mon, 13 Mar 2006 11:00:27 -0500
A.J..
Just to make sure I understand your problem. After adding a record, if you
click on the grid, the data in the group box isn't synced but otherwise it
is? (Just as an aside, for Adding a record, you may want to take a look at
the AddNew method of the Bindings, this will clear everything out for you in
your textboxes so you don't have to do it manually and will set any default
values if there ever are any).
Anyway, to your problem... have you verified that the data is being updated
in the db correctly? It looks like if it isn't, you're pulling the data
again so if it's bad, this will be a problem.
"A.J" <ajay.bisht@xxxxxxxxx> wrote in message
news:1142263798.652726.11330@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
There is a :
1.DataGrid: Columns-->firstName,LastName,EMail,PhoneNumber
2.GroupBox: controls-->4
TextBoxes(firstName,LastName,EMail,PhoneNumber)
3.Both DataGrid and GroupBox are binded to the same datasource(Dataset)
and Share the same BINDINGCONTEXT ie on clicking a record in the
grid;we can see the same record in the groupbox.
The problem is while doing edit operation ::
1.Once i clicked the EDIT button
2.text-box email,phonenumber will be enabled.
3.After editing i press the UPDATE button.
4.Though its being edited.
5. The PROBLEM is on clicking any row on the datagrid itz not showing
up in the groupbox.
There is some binding problem(On adding a new record there is no such
problem)
$$$$$$$$$$$$$$$$$$$$$$Edited
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
public string str = "Provider = Microsoft.JET.OLEDB.4.0;data
source=c:\\Access_DB\\contact.mdb;";
public string btn_string = "";
public OleDbConnection con;
public OleDbDataAdapter da;
public DataSet ds ;
private System.Windows.Forms.DataGrid dgrcontact;
$$$$$$$$$$$$$$$$$$$$$$Edited
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
btnAdd.Click += new EventHandler(Button_click);
btnCancel.Click +=new EventHandler(Button_click);
btnUpdate.Click +=new EventHandler(Button_click);
btnEdit.Click +=new EventHandler(Button_click);
btnCancel.Enabled = false;
btnUpdate.Enabled = false;
populate_datagrid();
bind_controls();
refresh();
}
$$$$$$$$$$$$$$$$$$$$$$Edited
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void bind_controls()
{
groupBox1.BindingContext = new BindingContext();
dgrcontact.BindingContext = new BindingContext();
dgrcontact.DataSource = ds;
dgrcontact.DataMember = "t_contact";
dgrcontact.BindingContext = groupBox1.BindingContext;
txtFirstName.DataBindings.Add("Text",ds,"t_contact.FirstName");
txtLastName.DataBindings.Add("Text",ds,"t_contact.LastName");
txtPhoneNumber.DataBindings.Add("Text",ds,"t_contact.PhoneNumber");
txtEMail.DataBindings.Add("Text",ds,"t_contact.EMail");
}
private void populate_datagrid()
{
con = new OleDbConnection(str);
con.Close();
con.Open();
string str1 = "select * from t_contact";
da = new OleDbDataAdapter(str1,con);
ds = new DataSet();
da.Fill(ds,"t_contact");
dgrcontact.DataSource = ds.Tables[0].DefaultView;
}
private void refresh()
{
disable();
btnAdd.Enabled = true;
btnCancel.Enabled = false;
btnUpdate.Enabled = false;
btnEdit.Enabled = true;
}
private void disable()
{
txtFirstName.Enabled = false;
txtLastName.Enabled = false;
txtPhoneNumber.Enabled = false;
txtEMail.Enabled = false;
}
private void enable()
{
txtFirstName.Enabled = true;
txtLastName.Enabled = true;
txtPhoneNumber.Enabled = true;
txtEMail.Enabled = true;
}
private void Button_click(object sender ,System.EventArgs e)
{
if (sender == btnAdd)
{
btn_string = "Add";
txtFirstName.Text = "";
txtLastName.Text = "";
txtPhoneNumber.Text = "";
txtEMail.Text = "";
enable();
btnAdd.Enabled = false;
btnCancel.Enabled = true;
btnUpdate.Enabled = true;
txtFirstName.Focus();
}
else if (sender == btnCancel)
{
refresh();
}
else if (sender == btnUpdate)
{
try
{
OleDbConnection con = new OleDbConnection(str);
con.Open();
string qry = "select * from t_contact";
OleDbDataAdapter da = new OleDbDataAdapter(qry,con);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
DataSet ds = new DataSet();
da.FillSchema(ds,SchemaType.Mapped,"t_contact");
da.Fill(ds,"t_contact");
DataTable dt = new DataTable();
dt = ds.Tables["t_contact"];
DataRow dr ;
if ( btn_string == "Add")
{
dr = dt.NewRow();
}
else //if(btn_string == "Edit")
{
dr = dt.Rows[dgrcontact.CurrentRowIndex];
}
dr.BeginEdit();
dr["FirstName"] = txtFirstName.Text;
dr["LastName"] = txtLastName.Text;
dr["EMail"] = txtEMail.Text;
dr["PhoneNumber"] = txtPhoneNumber.Text;
dr.EndEdit();
if (btn_string == "Add")
dt.Rows.Add(dr);
try
{
da.Update(dt);
MessageBox.Show("DataBase Being Updated");
}
catch( Exception e1)
{
MessageBox.Show("There is an exception" + e1.ToString());
}
refresh();
populate_datagrid();
}
catch ( Exception e2)
{
MessageBox.Show("Can't update " + e2.ToString());
}
finally
{
con.Close();
btn_string = "";
}
}
else if ( sender == btnEdit)
{
btn_string = "Edit";
txtFirstName.Enabled = false;
txtLastName.Enabled = false;
txtPhoneNumber.Enabled = true;
txtEMail.Enabled = true;
btnAdd.Enabled = false;
btnEdit.Enabled = false;
btnCancel.Enabled = true;
btnUpdate.Enabled = true;
txtEMail.Focus();
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
}
}
.
- Follow-Ups:
- Re: databinding problem
- From: A.J
- Re: databinding problem
- References:
- databinding problem
- From: A.J
- databinding problem
- Prev by Date: i have a question about rename table through ADOX with C++
- Next by Date: Re: @parameter - is it portable
- Previous by thread: databinding problem
- Next by thread: Re: databinding problem
- Index(es):
Loading