none
Two-way binding LINQ to SQL datacontext classes RRS feed

  • Question

  • Does anyone please know how to bind a textbox in design view to a LINQ to SQL datacontext class?
    Monday, August 17, 2009 8:41 PM

Answers

  • Hi JAHill,

    You can bind the textbox thiese two way to the datacontext class:

    1)BindindSource

            BindingSource bs;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DataClasses1DataContext context = new DataClasses1DataContext();
             
                bs = new BindingSource();
                bs.DataSource = context.Stus;
    
                 textBox1.DataBindings.Add(new Binding("Text", bs, "id"));
                 textBox2.DataBindings.Add(new Binding("Text", bs, "name"));
              
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                bs.Position -= 1;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                bs.Position += 1;
            }
        }

    2)BindingManagerBase


    BindingManagerBase bm;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DataClasses1DataContext context = new DataClasses1DataContext();
              
                 textBox1.DataBindings.Add(new Binding("Text", context.Stus, "id"));
                 textBox2.DataBindings.Add(new Binding("Text", context.Stus, "name"));
                 bm = this.BindingContext[context.Stus];
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                bm.Position -= 1;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                bm.Position += 1;
            }


    Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Wednesday, August 19, 2009 9:37 AM