By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,954 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,954 IT Pros & Developers. It's quick & easy.

Binding problem

P: n/a
A.J
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.

$$$$$$$$$$$$$$$$$$$$$$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_conta ct.PhoneNumber");
txtEMail.DataBindings.Add("Text",ds,"t_contact.EMa il");
}

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();
}
}

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

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)
}

}

Mar 11 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.