Binding problem

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



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

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

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top