首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

急求救,2005中用BindingSource把数据绑定到TextBox控件怎样保存?解决方法

2012-03-22 
急!求救,2005中用BindingSource把数据绑定到TextBox控件怎样保存? privateDataSetdsnewDataSet()private

急!求救,2005中用BindingSource把数据绑定到TextBox控件怎样保存?

private   DataSet   ds   =   new   DataSet();
private   BindingSource   BindingSource1   =   new   BindingSource();
private   SqlCommandBuilder   SqlCommandBuilder1;
private   SqlDataAdapter   da;
private   SqlConnection   cn;
string   cmdtext   =   "select   XH1,KHBM,ZBXH_KFLB,ZWMC,LXR,LXDH,DZ,SFZS,ZBXH_YWY,REMARK,ZDR,ZDRQ   from   JCZL_KHBM_M   where   XH1   =   "   +   xhh;
                        da   =   new   SqlDataAdapter(cmdtext,   cn);
                        SqlCommandBuilder1   =   new   SqlCommandBuilder(da);
                        da.Fill(ds);
                        BindingSource1.DataSource   =   ds;

                        this.textBox1.DataBindings.Add( "text ",   ds.Tables[0],   "XH1 ");
                       
                        this.textBox2.DataBindings.Add( "text ",   ds.Tables[0],   "KHBM ");
                        this.textBox3.DataBindings.Add( "text ",   ds.Tables[0],   "ZBXH_KFLB ");
                        this.textBox4.DataBindings.Add( "text ",   ds.Tables[0],   "ZWMC ");
                        this.textBox5.DataBindings.Add( "text ",   ds.Tables[0],   "LXR ");
                        this.textBox6.DataBindings.Add( "text ",   ds.Tables[0],   "LXDH ");
                        this.textBox7.DataBindings.Add( "text ",   ds.Tables[0],   "DZ ");
                        this.textBox8.DataBindings.Add( "text ",   ds.Tables[0],   "ZBXH_YWY ");
                        this.textBox9.DataBindings.Add( "text ",   ds.Tables[0],   "REMARK ");
                        this.textBox10.DataBindings.Add( "text ",   ds.Tables[0],   "ZDR ");
                        this.textBox11.DataBindings.Add( "text ",   ds.Tables[0],   "ZDRQ ");
                        this.checkBox1.DataBindings.Add( "Checked ",   ds.Tables[0],   "SFZS ");
                        this.comboBox1.DataBindings.Add( "text ",   ds.Tables[0],   "ZBXH_KFLB ");

这样子绑定,修改后用以下语句保存:
BindingSource1.EndEdit();
da.Update(ds, "JCZL_KHBM_M ");或者直接用da.Update(ds);


都不会报错,但是数据又没有被保存存,我之前用DataGridView绑定DataTable的数据都可以正常保存(不管是新建还是修改),但是这里用DataSet绑定TextBox为什么保存会没有反应呢?

[解决办法]
try..

BindingManagerBase类..
[解决办法]
simple example :

public partial class Form1 : Form
{
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
DataBind();
}
private void DataBind()
{
//绑定
SqlConnection con = new SqlConnection( "server=.;database=student;uid=sa;pwd=0421 ");
sda = new SqlDataAdapter( "select * from studentDetails ", con);
SqlCommandBuilder builder = new SqlCommandBuilder(sda);
sda.Fill(ds, "student ");
this.textBox1.DataBindings.Add( "Text ", ds, "student.sno ");
this.textBox2.DataBindings.Add( "Text ", ds, "student.sname ");
this.textBox3.DataBindings.Add( "Text ", ds, "student.sage ");
}

private void button1_Click(object sender, EventArgs e)
{
//前一个
this.BindingContext[ds, "student "].Position--;
}

private void button2_Click(object sender, EventArgs e)
{
//后一个
this.BindingContext[ds, "student "].Position++;
}

private void button3_Click(object sender, EventArgs e)
{
//更新数据库
BindingManagerBase bind = this.BindingContext[ds, "student "];
bind.EndCurrentEdit();
}
}

热点排行