Data synchronization problem with two DataGrid binding to the primary table and subtles in C #

xiaoxiao2021-03-06  38

In database programming, we often bind the primary table and subcons to two DataGrid, and some TEXTBOX's controls should display data in the corresponding DataGrid, and the data synchronization will become a problem. The solution to this problem is to use BindingManagerBase to manage data. DataSet added between the two tables in the relationship private void CreateRelation (Dataset ds, string relationname, string MasterTableName, string SlaveTableName, string ColumnName) // {ds.Tables [MasterTableName] .ChildRelations.Add (relationname, ds.Tables MasterTableName ] .Columns [color ", ds.tables [slavetablename] .COLUMNS [ColumnName]);} This adds a child relationship to the primary table MASTERTABLE. Establishing two BindingManagerBase: BindingManagerBase bmMaster = MasterGrid.BindingContext [ds, "MasterTableName"]; BindingManagerBase bmSlave = SlaveGrid.BindingContext [ds, "MasterTableName.relationname"]; after establishing good BindingManagerBase, designated the DataGrid the DataSource (preferably with SetDataBinding To make bindings, specify DataMember. If you use DataGrid.DataSource = DS.TABLES ["Tablename"], you can't get a role for the BindingManagerBase corresponding to the child table, I don't know what is the reason. It should be slavegrid.bindingContext [DS, "MASTERTABLENAME.RELATIONNAME"] problem) Mastergrid.setDatabase; slavegrid.setDataBinding (DS, "SlaveTableName"); this time Mastergrid has correspondingly corresponding to slavegrid. The TEXTBOX corresponding to the slavegrid is bound to TextBoxName.DATABINDINGS.ADD ("Text", DS, "Mastertablename.ReLATIONName.Name"); pay attention to the part of the underline, not using add ("text", DS.Tables [SlaveTableName], "Name") That is to say, establish a DATABINDINGMANAGER, binding data to DataSet and TextBox, is based on the corresponding relation to DataMember, instead of using only DataSource methods.

转载请注明原文地址:https://www.9cbs.com/read-76828.html

New Post(0)