EDN Admin
Well-known member
I have a form (form1) that has a DataSet d1 that contains a table t1. There is another form (form2) that has a DataSet Property called ds2 that contains a table t2.
Within form1 I create an instance of Form2. I then set the ds2 property of form2 to equal the ds1 (form2.ds2 = ds1);
I then call form2.ShowDialog
In form2 there is a checkbox that is bound to a field in the table t2;
When I click the checkbox (changing the value from true to false) the value changes within the row.
However if I output the rowstate of the row (that has changed) the rowstate shows Unchanged when it should (in theory) show modified;
I have the following copy paste project that I made to illustrate my problem, complete with console output to prove the state does not change even though the values DO change.
<pre class="prettyprint using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace RowStateChange
{
public partial class Form1 : Form
{
DataSet ds = new DataSet();
public Form1()
{
DataTable dt = new DataTable();
dt.TableName = "Table1";
dt.Columns.Add("Col1", typeof(String));
dt.Columns.Add("Col2", typeof(bool));
dt.Rows.Add(new object[] {"Test1", true});
dt.Rows.Add(new object[] { "Test2", true });
ds.Tables.Add(dt);
ds.AcceptChanges();
Button btn = new Button();
this.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
btn.Text = "Create Form2";
}
void btn_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Ds2 = ds;
f2.ShowDialog();
}
}
public class Form2 : Form
{
BindingSource bindingSource1 = new BindingSource();
CheckBox checkBox1 = new CheckBox();
DataSet ds2;
Button b1 = new Button();
public Form2()
{
InitializeComponent();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(12, 12);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(80, 17);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseVisualStyleBackColor = true;
this.Controls.Add(checkBox1);
this.Controls.Add(b1);
b1.Dock = DockStyle.Bottom;
b1.Text = "Print";
b1.Click += new EventHandler(b1_Click);
}
void b1_Click(object sender, EventArgs e)
{
PrintValues(ds2.Tables["Table1"]);
PrintRowState(ds2.Tables["Table1"]);
}
public void PrintRowState(DataTable dt)
{
foreach (DataRow dtrRow in dt.Rows)
{
Trace.WriteLine(dtrRow.RowState);
}
}
public void PrintValues(DataTable dt)
{
foreach (DataRow dtrRow in dt.Rows)
{
Trace.WriteLine(dtrRow["Col2"]);
}
}
public DataSet Ds2
{
get
{
return ds2;
}
set
{
ds2 = value;
bindingSource1.DataSource = ds2;
this.bindingSource1.DataMember = "Table1";
this.bindingSource1.DataSource = ds2;
checkBox1.DataBindings.Add(new System.Windows.Forms.Binding("Checked", bindingSource1, "Col2", true));
}
}
}
}
[/code]
<br/>
View the full article
Within form1 I create an instance of Form2. I then set the ds2 property of form2 to equal the ds1 (form2.ds2 = ds1);
I then call form2.ShowDialog
In form2 there is a checkbox that is bound to a field in the table t2;
When I click the checkbox (changing the value from true to false) the value changes within the row.
However if I output the rowstate of the row (that has changed) the rowstate shows Unchanged when it should (in theory) show modified;
I have the following copy paste project that I made to illustrate my problem, complete with console output to prove the state does not change even though the values DO change.
<pre class="prettyprint using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace RowStateChange
{
public partial class Form1 : Form
{
DataSet ds = new DataSet();
public Form1()
{
DataTable dt = new DataTable();
dt.TableName = "Table1";
dt.Columns.Add("Col1", typeof(String));
dt.Columns.Add("Col2", typeof(bool));
dt.Rows.Add(new object[] {"Test1", true});
dt.Rows.Add(new object[] { "Test2", true });
ds.Tables.Add(dt);
ds.AcceptChanges();
Button btn = new Button();
this.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
btn.Text = "Create Form2";
}
void btn_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Ds2 = ds;
f2.ShowDialog();
}
}
public class Form2 : Form
{
BindingSource bindingSource1 = new BindingSource();
CheckBox checkBox1 = new CheckBox();
DataSet ds2;
Button b1 = new Button();
public Form2()
{
InitializeComponent();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(12, 12);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(80, 17);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseVisualStyleBackColor = true;
this.Controls.Add(checkBox1);
this.Controls.Add(b1);
b1.Dock = DockStyle.Bottom;
b1.Text = "Print";
b1.Click += new EventHandler(b1_Click);
}
void b1_Click(object sender, EventArgs e)
{
PrintValues(ds2.Tables["Table1"]);
PrintRowState(ds2.Tables["Table1"]);
}
public void PrintRowState(DataTable dt)
{
foreach (DataRow dtrRow in dt.Rows)
{
Trace.WriteLine(dtrRow.RowState);
}
}
public void PrintValues(DataTable dt)
{
foreach (DataRow dtrRow in dt.Rows)
{
Trace.WriteLine(dtrRow["Col2"]);
}
}
public DataSet Ds2
{
get
{
return ds2;
}
set
{
ds2 = value;
bindingSource1.DataSource = ds2;
this.bindingSource1.DataMember = "Table1";
this.bindingSource1.DataSource = ds2;
checkBox1.DataBindings.Add(new System.Windows.Forms.Binding("Checked", bindingSource1, "Col2", true));
}
}
}
}
[/code]
<br/>
View the full article