|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
databinding problem1.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(); } } @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ } } 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. Show quote "A.J" <ajay.bi***@gmail.com> wrote in message news:1142263798.652726.11330@p10g2000cwp.googlegroups.com... > 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(); > } > } > > > > @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ > > > } > > } > hi buddy,
tkx4such a quick reply.Actually the probs is when m trying to edit a record(Everything is fine when m adding a new record ie there is 1to1 relationship b/w datagrid and groupbox in this case) and i have checked the records in the DB .I find them ok My guess then is that you have a value that's null or something like that
which may be causing this. I know that I've bound stuff to radiobuttons before and the value couldn't be set. It didn't blow up but the binding just didn't update. Is this the symptom you're having? Show quote "A.J" <ajay.bi***@gmail.com> wrote in message news:1142267641.285941.284630@u72g2000cwu.googlegroups.com... > hi buddy, > tkx4such a quick reply.Actually the probs is when m > trying to edit a record(Everything is fine when m adding a new record > ie there is 1to1 relationship b/w datagrid and groupbox in this case) > and i have checked the records in the DB .I find them ok > buddy, i just checked there are no null records and while going through
it i got to know that the above prob. is with addition of records also... Right now I would change the code for the Add button to use call the AddNew
method of the BindingManager. Then call Update and see if that doesn't address the issue. Show quote "A.J" <ajay.bi***@gmail.com> wrote in message news:1142271667.401037.71990@i39g2000cwa.googlegroups.com... > buddy, i just checked there are no null records and while going through > it i got to know that the above prob. is with addition of records > also... > AJ,
I assume that you use call the pencil the Edit button. Be aware that data is pushed into a datarow after an EndCurrentEdit (version 1.x), EndEdit (version 2.0) or a change of the datarow in a datagid/datagridview If you use by instance the bindingnavigator and disable all buttons on the grid, than you can avoid all of this. I hope this helps, Cor Show quote "A.J" <ajay.bi***@gmail.com> schreef in bericht news:1142263798.652726.11330@p10g2000cwp.googlegroups.com... > 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(); > } > } > > > > @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ > > > } > > } >
Other interesting topics
Passing morethan 32 KB Data to a Stored Procedure using ODP.Net Provider.
DataAccessApplicationBlock without stored procedures? Enterprise Library - DataAccessApplicationBlock - MySql/DB2 DataTable primary key - unique and no nulls by default? MS Access - Get new Autonumber value after DataAdapter.Update |
|||||||||||||||||||||||