?
Solved

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

Posted on 2009-04-26
10
Medium Priority
?
398 Views
Last Modified: 2013-11-07
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!]
0
Comment
Question by:smickell
  • 7
  • 2
9 Comments
 
LVL 13

Expert Comment

by:dungla
ID: 24341254
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
 

Author Comment

by:smickell
ID: 24350882
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
 
LVL 13

Expert Comment

by:dungla
ID: 24352818
Your project is Web application or Window application?
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:smickell
ID: 24353602
C#. 2.0, windows form-based.
0
 
LVL 13

Expert Comment

by:dungla
ID: 24353763
I think you can use DataGrid and DataGridTextBox to display data. Then you can edit/update via DataGrid and DataSource.
0
 
LVL 13

Expert Comment

by:dungla
ID: 24353770
Check out this link for further information
http://www.codeproject.com/KB/grid/control_datagrid.aspx
0
 
LVL 13

Expert Comment

by:dungla
ID: 24353829
Or you can use
CustomControl[i].txtNotes.DataBindings.Add("Tag", this.imageBindingSource, "IdentityField", true);

to bind data to Tag property
0
 
LVL 13

Expert Comment

by:dungla
ID: 24353850
I forgot, you need to loop thru each row of source, then binding data for associated text box
0
 
LVL 13

Accepted Solution

by:
dungla earned 2000 total points
ID: 24354089
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month16 days, 1 hour left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question