M
Magnus Blomberg
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();
MessageBox.Show(Convert.ToString(ds.HasChanges()));
}
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();
MessageBox.Show(Convert.ToString(ds.HasChanges()));
}