|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Save data from control to datasourceI am trying to use simple binding to enter text in a textbox and save it to the datasource. When I enter text in the textbox and press a save button, this text is not saved, because the datasource don't think the row has been changed. Here below is a simple code to reproduce the problem. Paste this code in an empty form and run the project. Then change the text in the textbox. Then press the button (could be the save button). This should give the answer 'true', that the dataset has been changed, but it doesn't. If I then in the combobox change to 'Second', and press the button, it says 'true', but I don't want to change record before saving (especially if I only have one record). Regards Magnus DataSet ds = new DataSet(); DataTable dt = new DataTable(); TextBox txt = new TextBox(); ComboBox cmb = new ComboBox(); Button btn = new Button(); private void Form1_Load(object sender, EventArgs e) { dt.Columns.Add(new DataColumn("Name")); dt.Columns.Add(new DataColumn("ID")); dt.Columns.Add(new DataColumn("FreeText")); dt.Rows.Add(new Object[] { "First", 1, "First freetext" }); dt.Rows.Add(new Object[] { "Second", 2, "Second freetext" }); dt.Rows.Add(new Object[] { "Third", 3, "Third freetext" }); dt.AcceptChanges(); ds = new DataSet(); ds.Tables.Add(dt); cmb.DataSource = dt; cmb.DisplayMember = "Name"; cmb.ValueMember = "ID"; this.Controls.Add(cmb); txt.DataBindings.Add(new Binding("Text", dt, "FreeText")); txt.Top = 50; this.Controls.Add(txt); btn.Top = 100; btn.Text = "HasChanges"; btn.Click += new EventHandler(btn_Click); this.Controls.Add(btn); } void btn_Click(object sender, EventArgs e) { txt.DataBindings[0].WriteValue(); MessageBox.Show(Convert.ToString(ds.HasChanges())); } ---------------------------------------------------------------------------- ----------------- Hi,
Show quote "Magnus Blomberg" <magnus.blomb***@skanska.se> wrote in message You're forcing the TextBox value into the DataSource ( DataTable here ). news:uMf24v3FGHA.1676@TK2MSFTNGP09.phx.gbl... > Hi there! > > I am trying to use simple binding to enter text in a textbox and save it > to > the datasource. > When I enter text in the textbox and press a save button, this text is not > saved, because the datasource don't think the row has been changed. > Here below is a simple code to reproduce the problem. > > Paste this code in an empty form and run the project. > > Then change the text in the textbox. Then press the button (could be the > save button). > This should give the answer 'true', that the dataset has been changed, but > it doesn't. > If I then in the combobox change to 'Second', and press the button, it > says > 'true', but I don't want to change record before saving (especially if I > only have one record). > > Regards Magnus > > DataSet ds = new DataSet(); > > DataTable dt = new DataTable(); > > TextBox txt = new TextBox(); > > ComboBox cmb = new ComboBox(); > > Button btn = new Button(); > > private void Form1_Load(object sender, EventArgs e) > > { > > dt.Columns.Add(new DataColumn("Name")); > > dt.Columns.Add(new DataColumn("ID")); > > dt.Columns.Add(new DataColumn("FreeText")); > > dt.Rows.Add(new Object[] { "First", 1, "First freetext" }); > > dt.Rows.Add(new Object[] { "Second", 2, "Second freetext" }); > > dt.Rows.Add(new Object[] { "Third", 3, "Third freetext" }); > > dt.AcceptChanges(); > > ds = new DataSet(); > > ds.Tables.Add(dt); > > > > cmb.DataSource = dt; > > cmb.DisplayMember = "Name"; > > cmb.ValueMember = "ID"; > > this.Controls.Add(cmb); > > > > txt.DataBindings.Add(new Binding("Text", dt, "FreeText")); > > txt.Top = 50; this.Controls.Add(txt); > > > > btn.Top = 100; > > btn.Text = "HasChanges"; > > btn.Click += new EventHandler(btn_Click); > > this.Controls.Add(btn); > > } > > void btn_Click(object sender, EventArgs e) > > { > > txt.DataBindings[0].WriteValue(); But a DataRow supports delayed editing, meaning that it will not report changes to the DataRow until EndEdit has been called if BeginEdit was called. (BeginEdit & EndEdit is implicitly called when you browse records). If there is no BindingSource between the DataSource and Control, then you need to use a CurrencyManager which you can get from a BindingContext, eg.: BindingContext[dt].EndCurrentEdit(); That will push all values into the DataSource and call EndEdit which will now flag the row as changed. If there is a BindingSource between them, you simply call someBindingSource.EndEdit(); HTH, Greetings Show quote > > MessageBox.Show(Convert.ToString(ds.HasChanges())); > > } > > > ---------------------------------------------------------------------------- > ----------------- > > > |
|||||||||||||||||||||||