将usercontrol文本框内容保存回其bindingsource

时间:2014-01-16 14:11:40

标签: c# winforms

我在Winforms工作,现在卡住了;我已经为文本框创建了一个自定义用户控件,如下面的代码所示:

public partial class TextBox : UserControl
{
    public TextBox()
    {
        InitializeComponent();
        txtTEXT.Text = "";
    }

    [DefaultValue("Text")]
    public string Caption 
    { 
        get { return lblCAPTION.Text; } 
        set { lblCAPTION.Text = value;} 
    }

    [Browsable(true)]
    public new string Text
    {
        get { return txtTEXT.Text; }
        set { txtTEXT.Text = value; }
    }

    [Browsable(true),DefaultValue(100)]
    public int Caption_Width
    {
        get { return lblCAPTION.Width; }
        set { lblCAPTION.Width = value; }
    }

    private void txtTEXT_TextChanged(object sender, EventArgs e)
    {
        if (this.txtTEXT.Text.StartsWith("textBox"))
            this.txtTEXT.Text = "";
    }

    public new BorderStyle BorderStyle
    {
        get { return txtTEXT.BorderStyle; }
        set { txtTEXT.BorderStyle = value; }
    }

    public bool PasswordChar 
    {
        get { return txtTEXT.UseSystemPasswordChar; } 
        set {txtTEXT.UseSystemPasswordChar = value ;}
    }

    public bool m_Mandatory = false;
    public bool Mandatory 
    {
        get { return m_Mandatory; }
        set 
        {
                m_Mandatory= value;
                lblAMENDATORY.Visible = m_Mandatory;
        } 

    }

    private void TextBox_Resize(object sender, EventArgs e)
    {
        this.Height = txtTEXT.Height;
    }
}

然后在另一种形式中,我已将此文本框绑定,如下面的代码所示:

           BindingSource bs = new BindingSource();
    BindingSource bsDet = new BindingSource();
    DataSet data;
    SqlDataAdapter masterDataAdapter;
    SqlDataAdapter detailsDataAdapter;

        SqlConnection connection = new SqlConnection(connectionString);

        // Create a DataSet.
        data = new DataSet();

        // Add data from the Customers table to the DataSet.
        masterDataAdapter = new SqlDataAdapter("select * from RFID_MAS", connection);
        masterDataAdapter.Fill(data, "RFID_MAS");

        // Add data from the Orders table to the DataSet.
        detailsDataAdapter = new SqlDataAdapter("select * from RFID_DET", connection);
        detailsDataAdapter.Fill(data, "RFID_DET");

        // Establish a relationship between the two tables.
        DataRelation relation = new DataRelation("MyRelation",                  data.Tables["RFID_MAS"].Columns["SEQ_NO"],data.Tables["RFID_DET"].Columns["MAS_SEQ"]);
        data.Relations.Add(relation);

        // Bind the master data connector to the Customers table.
        bs.DataSource = data;
        bs.DataMember = "RFID_MAS";

        // Bind the details data connector to the master data connector,
        // using the DataRelation name to filter the information in the 
        // details table based on the current row in the master table. 
        bsDet.DataSource = bs;
        bsDet.DataMember = "MyRelation";
        // textBox1 is default Windows TextBox
        textBox1.DataBindings.Add(new Binding("Text", this.bs, "RFID_NO", true,               DataSourceUpdateMode.OnValidation));
        //  textBox2 is my ouwn usercontrol textbox
        textBox2.DataBindings.Add(new Binding("Text", this.bs, "SEQ_NO",true,                  DataSourceUpdateMode.OnValidation));

        dataGridView2.DataSource = bsDet;
        dataGridView1.AutoResizeColumns();

        dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

        bindingNavigator1.BindingSource = bs;
        //bs.DataSource = dset.Tables[0];
        //dataGridView1.DataSource = bs;

然后我将文本框数据保存为:

 this.Validate();
        this.bs.EndEdit();
        SqlCommandBuilder cm = new SqlCommandBuilder(masterDataAdapter);
        this.masterDataAdapter.Update(data.Tables[0]);

现在我的问题是,当我添加新记录时, 来自TextBox1的数据(即Windows默认文本框)在BindingSource中得到更新, 但是来自TextBox2的数据(即我自己的用户控件文本框)没有在Bindingsource中更新。

如果我错过了什么,请帮助我。 非常感谢你。

0 个答案:

没有答案