C# Muliple Textboxes Databinding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devett
    New Member
    • Oct 2008
    • 1

    C# Muliple Textboxes Databinding

    I want to create 4 textboxes that will store a lastname from a database. I each of the 4 textboxes to display at first the first 4 lastnames example: Textbox1 will have the bind data for the first record Henderson then Textbox2 second lastName Jones from the same DataTable and so on. I then will put a navigator to go through the records incrementing the 4 boxes with the next records. Can this be done and if so, can someone steer me in the right direction. I know how to start, at least I think.

    using System;
    using System.Collecti ons;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;

    namespace CustomControlsD ataBinding
    {
    public partial class Form1 : Form
    {
    private BindingManagerB ase techBinding;
    private BindingManagerB ase techBinding1;
    private ArrayList drivers = new ArrayList();

    public Form1()
    {
    InitializeCompo nent();
    }

    private void Form1_Load(obje ct sender, EventArgs e)
    {
    DatabaseManager tectDB = new DatabaseManager ();

    DataTable dt = tectDB.GetTechi nicians();

    foreach (spDriver item in DatabaseManager .GetListOfAssig nedDrivers)
    {
    drivers.Add(ite m.lastName);
    }

    textBox1.DataBi ndings.Add("Tex t", dt, "lastName") ;
    textBox2.DataBi ndings.Add("Tex t", dt, "lastName") ;
    textBox3.DataBi ndings.Add("Tex t", dt, "lastName") ;
    textBox4.DataBi ndings.Add("Tex t", dt, "lastName") ;

    techBinding = this.BindingCon text[dt];

    techBinding1 = this.BindingCon text[dt1];

    techBinding.Pos itionChanged += new EventHandler(te chBinding_Posit ionChanged);
    }

    private void button1_Click(o bject sender, EventArgs e)
    {
    techBinding.Pos ition--;
    }

    private void button2_Click(o bject sender, EventArgs e)
    {
    techBinding.Pos ition++;
    textBox4.Text = drivers[2].ToString();
    }

    private void techBinding_Pos itionChanged(ob ject sender, System.EventArg s e)
    {
    if (techBinding.Po sition == techBinding.Cou nt - 1)
    {
    cmdNext.Enabled = false;
    }
    else
    {
    cmdNext.Enabled = true;
    }

    if (techBinding.Po sition == 0)
    {
    cmdPrev.Enabled = false;
    }
    else
    {
    cmdPrev.Enabled = true;
    }
    }
    }
    }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by devett
    I want to create 4 textboxes that will store a lastname from a database. I each of the 4 textboxes to display at first the first 4 lastnames example: Textbox1 will have the bind data for the first record Henderson then Textbox2 second lastName Jones from the same DataTable and so on. I then will put a navigator to go through the records incrementing the 4 boxes with the next records. Can this be done and if so, can someone steer me in the right direction. I know how to start, at least I think.
    [CODE=c]
    using System;
    using System.Collecti ons;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;

    namespace CustomControlsD ataBinding
    {
    public partial class Form1 : Form
    {
    private BindingManagerB ase techBinding;
    private BindingManagerB ase techBinding1;
    private ArrayList drivers = new ArrayList();

    public Form1()
    {
    InitializeCompo nent();
    }

    private void Form1_Load(obje ct sender, EventArgs e)
    {
    DatabaseManager tectDB = new DatabaseManager ();

    DataTable dt = tectDB.GetTechi nicians();

    foreach (spDriver item in DatabaseManager .GetListOfAssig nedDrivers)
    {
    drivers.Add(ite m.lastName);
    }

    textBox1.DataBi ndings.Add("Tex t", dt, "lastName") ;
    textBox2.DataBi ndings.Add("Tex t", dt, "lastName") ;
    textBox3.DataBi ndings.Add("Tex t", dt, "lastName") ;
    textBox4.DataBi ndings.Add("Tex t", dt, "lastName") ;

    techBinding = this.BindingCon text[dt];

    techBinding1 = this.BindingCon text[dt1];

    techBinding.Pos itionChanged += new EventHandler(te chBinding_Posit ionChanged);
    }

    private void button1_Click(o bject sender, EventArgs e)
    {
    techBinding.Pos ition--;
    }

    private void button2_Click(o bject sender, EventArgs e)
    {
    techBinding.Pos ition++;
    textBox4.Text = drivers[2].ToString();
    }

    private void techBinding_Pos itionChanged(ob ject sender, System.EventArg s e)
    {
    if (techBinding.Po sition == techBinding.Cou nt - 1)
    {
    cmdNext.Enabled = false;
    }
    else
    {
    cmdNext.Enabled = true;
    }

    if (techBinding.Po sition == 0)
    {
    cmdPrev.Enabled = false;
    }
    else
    {
    cmdPrev.Enabled = true;
    }
    }
    }
    }
    [/CODE]
    Let me make sure I understand your goal, before understanding the problem.

    You want to bind a series of text boxes to a datasource.
    You want all four text boxes bound to the SAME field of the datasource.
    You want each box to show a successive position from the datasource: IE. Box 1 shows item 1, box 2 shows item 2, box 3 item 3, box 4 shows item 4.
    But you are going to put a navigation control on the text box so you can browse the list.

    Does that sound like the goal?

    Why not just bind to a ComboBox instead of a TextBox? That will show all the items, let the user pick an item, and has built in navigation [rather than coding all your own] while only taking the real estate of a text box when not being navigated.

    I suppose you could setup four ComboBoxes with the same binding and set their .SelectedIndex value to 1-4 respectively when the form loads. That would get you the individual items displayed on screen when it first opens.

    Regards,
    tlhIn'toQ

    PS: Notice how the use of the [code] tags makes your snippet much easier to read/recognize as code? [More on tags ]

    Comment

    Working...