积极答复者
数据绑定的问题

问题
-
public partial class dataBind : Form
{
public dataBind()
{
InitializeComponent();
}private BindingManagerBase BindingManager;
private TB tb1 = new TB();
DataSet Ds = new DataSet();private void dataBind_Load(object sender, EventArgs e)
{
this.Controls.Add(tb1);
tb1.Left = textBox1.Left;
tb1.Top = textBox1.Top + 60;
Ds.ReadXml("charData.xml");
Ds.AcceptChanges();
BindingManager = this.BindingContext[Ds.Tables[0]];
textBox1.DataBindings.Add("Text", Ds.Tables[0], "hz");//如果绑定到Text再按按钮在下面的DataGridView就不会有已改动的记录出现,请问这是为什么?
tb1.DataBindings.Add("ValueText", Ds.Tables[0], "wbdm");
}private void button1_Click(object sender, EventArgs e)
{
BindingManager.Position++;
if(Ds.GetChanges() != null)
dataGridView1.DataSource = Ds.GetChanges().Tables[0];
}
}class TB : TextBox
{
private string valueText;
public string ValueText
{
get { return valueText; }
set
{
valueText = value;
this.Text = "Add Text" + value;
}
}
}这样存在一个问题,就是数据移到下一条后,虽然值没有修改,但是表中的这行就会是已修改的状态,而直接绑定到文本框的Text属性时则不会出现这个问题,不知这是怎么回事?
答案
-
你好!
在 ValueText 中增加判断,值相同时不做赋操作。
public string ValueText
{
get { return valueText; }
set
{
if (value != valueText)
{
valueText = value;
this.Text = "Add Text" + value;
}
}
}
知识改变命运,奋斗成就人生!- 已标记为答案 KeFang Chen 2010年4月20日 2:53
-
你好!
在 ValueText 的 Set 块中增加判断,值不同时才进行赋值操作。
public string ValueText { get { return valueText; } set { if (value != valueText) // 值不同时赋值 { valueText = value; this.Text = "Add Text" + value; } } }
知识改变命运,奋斗成就人生!- 已标记为答案 KeFang Chen 2010年4月20日 2:53
全部回复
-
你好!
在 ValueText 中增加判断,值相同时不做赋操作。
public string ValueText
{
get { return valueText; }
set
{
if (value != valueText)
{
valueText = value;
this.Text = "Add Text" + value;
}
}
}
知识改变命运,奋斗成就人生!- 已标记为答案 KeFang Chen 2010年4月20日 2:53
-
你好!
在 ValueText 的 Set 块中增加判断,值不同时才进行赋值操作。
public string ValueText { get { return valueText; } set { if (value != valueText) // 值不同时赋值 { valueText = value; this.Text = "Add Text" + value; } } }
知识改变命运,奋斗成就人生!- 已标记为答案 KeFang Chen 2010年4月20日 2:53