.NET Forum / Windows Forms / WinForm General / February 2004
Binding textboxes to dataset relationship
|
|
Thread rating:  |
M K - 25 Feb 2004 16:41 GMT I have 2 textboxes. Each show a field from a different table (2 tables). The dataset has a relationship defined. I want my navigation button to navigate through the records. So that when the record in one table is selected, the textboxes bound to the other table change accordingly
Also, there might not be a relationship associated between the tables for certain records
any ideas?
"Ying-Shen Yu[MSFT]" - 26 Feb 2004 03:15 GMT Hi mark,
From my understanding, Your scenario is like you have a two tables in master-detail relationship, you would like to bind a control to the parent table to let user select a certain record, then show the child rows in some textbox one record a time and let them navigate through the child rows. If I misunderstood your problem, please feel free to let me know.
Based on my research, since you have defined a relation ship between these two tables, you may Bind the "Text" property of the TextBoxes to "DataSet - DataTable\DataRelation\PropertyName",
Then when you change the parent row, the child rows will changed automatically, the content in the textboxes will be reset to the first record of the child rows. You can navigate the child rows collection by changing the Position property of the BindingManagerBase class on that collection. Here is a short snippet for navigating to the next record:
BindingManagerBase bm = BindingContext[dataSet11,"Suppliers.SuppliersProducts"]; if (bm.Count == bm.Position + 1) { bm.Position += 1; } else { button1.Enabled = false; }
By the way, to reset the navigation state, maybe you need add an event handler to BindingManagerBase.PositionChanged of your parent BindingManagerBase object.
Does it solve your problem? Please feel free to reply this thread, if you have anything unclear about this issue. Thanks!
Best regards,
Ying-Shen Yu [MSFT] Microsoft community Support Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties and confers no rights. This mail should not be replied directly, please remove the word "online" before sending mail.
"Ying-Shen Yu[MSFT]" - 26 Feb 2004 03:20 GMT Sorry, a typo in the snippet, should be : <code> BindingManagerBase bm = BindingContext[dataSet11,"Suppliers.SuppliersProducts"]; if (bm.Count == bm.Position + 1) { button1.Enabled = false; } else{ bm.Position += 1; } </code>
Best regards,
Ying-Shen Yu [MSFT] Microsoft community Support Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties and confers no rights. This mail should not be replied directly, please remove the word "online" before sending mail.
M K - 26 Feb 2004 14:01 GMT Yes, I do have two tables, SubjectData (Parent) and Orders (Child). However, I want to be able to navigate through the orders. There may be Orders with no SubjectData associated. There may also be more than one Order for a single row of SubjectData. The Orders coorespond to frames on film. So Orders must be sequential. But not all the subjects will be on the film
I bind to Orders like this tbRecNum.DataBindings.Add(New Binding("Text", Me.dsJobData, "Orders.RecID")
And to SubjectData like this newTb.DataBindings.Add(New Binding("Text", Me.dsJobData, "SubjectData.Firstname")
I navigate through the records like this Me.BindingContext(Me.dsJobData, "Orders").Position +=
But this of course only updates the textboxes that represent Order data. My Relation ship is created like this ds.Relations.Add(New DataRelation("OrdersToSubjects", ds.Tables(2).Columns("RecID"), ds.Tables(1).Columns("RecID"))) 'Table 2 being SubjectData and 1 being Order
Thank you for your time. I hope this clarifies. I get very confused with this Textbox binding, but that is what the app calls for
"Ying-Shen Yu[MSFT]" - 27 Feb 2004 09:31 GMT Hi mark,
Let me understand your problem again. You want to navigate in the Orders table and show the corresponding SubjectData for the current row in Orders table. And The Orders table have records that have no corresponding row in the SubjectData table. Based on my research, databinding doesn't support this scenario.
Before researching on how to resolve this issue, could you give me some more information on the datatable( such as the keys on the two tables etc). I'd also like to confirm the detail relation between these two tables: 1: every Order has no more than one SubjectData. 2: is there a situation that an SubjectData row exists, but has no related Orders?
Another thought on this issue would be define a new DataTable which contains all the column you need then join these two table when filling the DataTable. Suppose you have the table definition like below: SubjectData: 1.SubjectID 2.Subject
Orders: 1.OrderID 2. SubjectID 3. OrderInfo Then the SQL command to get the data would be
Select OrderID, Subject , SubjectID, Orderinfo from SubjectData, Orders where SubjectData.SubjectID = Orders.SubjectID
Note since you joined two tables, you need also provide the Insert/Delete/Update SQL command to enable these function on the datatable.
Then all data are in one table, the databinding would be easy.
Is this a possible resolution?
If you have anything unclear on this issue please feel free to reply this thread to let me know.
Thanks!
Best regards,
Ying-Shen Yu [MSFT] Microsoft community Support Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties and confers no rights. This mail should not be replied directly, please remove the word "online" before sending mail.
M K - 27 Feb 2004 13:56 GMT SubjectDat RecID Firstname Lastname Address..
Order OrderID RecID Sequence Roll Frame Package1 Quantity1 Package2...
Yes, there will be SubjectData that does not have an Order There will be Orders (cooresponding to Frames on Film) that may not have SubjectData. New Orders do not have their SubjectData (RecID) set yet. Some Orders will be Slates, blank frames that we need to keep a record of, thus no SubjectData record
As far as updating. The user may need to update either the record in the SubjectData table, Order table, or both
"Ying-Shen Yu[MSFT]" - 28 Feb 2004 10:48 GMT Hi mark,
From your description, as far as I know, databinding could not help in this scenario, You need write addintion code to set the parent table to the corresponding data row. Ofcourse, databinding will still be helpful to sync data between controls and there source tables.
I think you may try handling the PositionChanged event of the Orders table CurrencyManager, then manually set the position of the SubjectData table to the correct row, Here is a simple sample to show this idea, it's far from complete you need also handle some other events to make it work correctly.However I hope it will show you the idea about this way. <code> private void Form1_Load(object sender, System.EventArgs e) { oleDbDataAdapter2.Fill(dataSet12); oleDbDataAdapter1.Fill(dataSet12); //set the sort order in order to use Find on DataView dataSet12.SubjectData.DefaultView.Sort = "RecID ASC"; BindingContext[dataSet12,"Orders"].PositionChanged +=new EventHandler(Form1_PositionChanged); }
private void Form1_PositionChanged(object sender, EventArgs e) { DataRowView dvr = BindingContext[dataSet12,"Orders"].Current as DataRowView; BindingManagerBase bm = BindingContext[dataSet12,"SubjectData"];
//check if the primary key exists in the DataTable, //if yes, simply set position to that row //if not, currently I use a simple workaround by always add an empty row and set //the position to that row int ret = dataSet12.SubjectData.DefaultView.Find(dvr["RecID"]);
if (ret != -1) bm.Position = ret; else bm.Position = bm.Count -1; } </code>
Hope it helps!
Best regards,
Ying-Shen Yu [MSFT] Microsoft community Support Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties and confers no rights. This mail should not be replied directly, please remove the word "online" before sending mail.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|