Setting the BindingSource of several separate controls from one data source at runtime

I have several FlowLayoutPanels on a form that get populated with a control (TextBox) at runtime. All the data is stored in one table but the actual controls are put in different FlowLayoutPanels depending on what category they belong to.

I have imageTableAdapter.FillByPatientID(this.dataSet1.Image) to get me all the record entries for one patient record. For each DataRow in this.dataSet1.Image I create one TextBox control on the form. But I don't know how to set the bindings.

CustomControl[i].txtNotes.DataBindings.Add("Text", this.imageBindingSource, "Notes", true);
                        
If I just keep the above as 'this.imageBindingSource' changing the text of one field changes it for all controls in the form. (The changes are successfully made by the EndEdit() and AcceptChanges() methods btw). How do I know what BindingSource row I should set for each control?

[question re-worded 29/04 to simplify!]
smickellAsked:
Who is Participating?
 
dunglaConnect With a Mentor Commented:
I think proper solution could be:

// display data to textbox
int nLastY = 0;
foreach (DataRow row in dtbSource.Rows)
{
    // create text box
    TextBox txt = new TextBox();
    txt.Text = row["Col1"].ToString();
    txt.Tag = row["Col2"];
    // add to next row
    txt.Location = new Point(txt.Location.X, nLastY + txt.Height);
    // add to panel
    panel1.Controls.Add(txt);
    nLastY = txt.Location.Y;
}

// Save button click event
// collect data from panel
foreach (Control control in panel1.Controls)
{
    if (control.GetType() != typeof(TextBox))
        continue;
    string text = control.Text;
    int nID = Convert.ToInt32(control.Tag);
    // find row by id
    DataRow[] rows = dtbSource.Select("Col2 = " + nID);
    if (rows.Length > 0)
        rows[0]["Col1"] = text; // set new text;
}
// update to database via Adapter.Update method
0
 
dunglaCommented:
If your record have an ID (let say that "rowID" field), then you can bind the value of rowID field to the Tag of text box. Later, if you want to check which row is belong to text box, just retrieve the Tag value, find by rowID in datasource
0
 
smickellAuthor Commented:
Thanks dungla for the reply.
Each record does have an ImageID. Unfortunately it's the binding I'm trying to set up in the first place that I'm having trouble with - I don't know how to bind the ImageID field to the Tag of the control because I can't figure out what to pass to the DataBindings.Add() method I outlined above. It's maybe very easy and I'm just missing the point, or could be something very complicated?

What I don't understand is that how, say, a DataGridView can have multiple rows bound to one BindingSource but I can't do it with my group of controls.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
dunglaCommented:
Your project is Web application or Window application?
0
 
smickellAuthor Commented:
C#. 2.0, windows form-based.
0
 
dunglaCommented:
I think you can use DataGrid and DataGridTextBox to display data. Then you can edit/update via DataGrid and DataSource.
0
 
dunglaCommented:
Check out this link for further information
http://www.codeproject.com/KB/grid/control_datagrid.aspx
0
 
dunglaCommented:
Or you can use
CustomControl[i].txtNotes.DataBindings.Add("Tag", this.imageBindingSource, "IdentityField", true);

to bind data to Tag property
0
 
dunglaCommented:
I forgot, you need to loop thru each row of source, then binding data for associated text box
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.