代码之家  ›  专栏  ›  技术社区  ›  DRapp

绑定到表,当数据行内容更改时刷新窗体控件

  •  0
  • DRapp  · 技术社区  · 15 年前

    我正在使用C#.Net和SqlCe数据库开发一个手持设备。我有一个有多个面板的窗体,每个面板都有自己的文本框控件组。构建表单时,我会自动将每个textbox控件绑定到从数据库查询的表中与其匹配的列名。例如:txtFld1=ResultTable.Fld1,txtFld2=ResultTable.Fld2,等等。所有这些工作都没有问题。如果我在表中查询给定的记录ID,那么表单刷新没有问题。如果单击窗体上的按钮并手动更改一个或多个字段表中单行的值,则窗体不会反映这些更改。。。前任:

    DataRow oDR = MySQLResultTable.Rows[0];
    oDR["Fld1"] = "Something";
    oDR["Fld2"] = "else";
    oDR["Fld3"] = "changed";
    

    我错过了什么。

    谢谢

    在它的简化格式中,因为我确定了控件的名称并在表中找到了相应的列名

    public void BindControlsToDataTable(Control Page, DataTable oTbl )
    {
       String cObjName;
       foreach (Control oCtrl in Page.Controls)
       {
          // if this control has other controls, bind them too...
          if (oCtrl.Controls.Count > 0)
             BindControlsToDataTable(oCtrl, oTbl);
    
          // Now, try to do dynamic binding.  The controls are created
          // based on a 3 char prefix such as "txt", "cbo", "chk", etc
          // with the remainder as the object name they represent...
          // ex: txtDescription, txtFirstName, txtLastName, chkIsOk, etc..
          cObjName = oCtrl.Name.Substring(3);
          if (oTbl.Columns[lcName] != null)
          {
             oCtrl.DataBindings.Clear();
             // we are binding the "TEXT" property of the control
             // to the column name in the table/row
             oCtrl.DataBindings.Add("Text", oTbl, cObjName,true, 
                 DataSourceUpdateMode.OnPropertyChanged);
          }
       }
    }
    

    HTH公司

    1 回复  |  直到 15 年前
        1
  •  0
  •   DRapp    15 年前