【问题标题】:automatically make a display/editing control for data from database自动对数据库中的数据进行显示/编辑控制
【发布时间】:2013-04-29 10:51:49
【问题描述】:

我可以用来显示数据库中的数据的最佳控制/方式是什么,用户可以在其中编辑一些数据而不能编辑其他数据。 我不需要使用数据网格。 我需要从数据库中读取每个数据并自动为其制作显示/编辑控件..

类似:

【问题讨论】:

    标签: c# .net database winforms controls


    【解决方案1】:
    DataTable myTable = myRow.Table;
    foreach (DataColumn nextCol in myTable.Columns)
    {
      // filter out Name, Age, ID, Salary
      // if nextCol is Name, Age, ID, or Salary
      // continue
    
      // add a label and text box for next column
      Label nextLabel = new Label();
      nextLabel.Text = nextCol.Caption;
      // add next label to your control
    
      // add a text box for the next column
      TextBox nextTb = new TextBox();
      // add next text box to your control
    
      // assume your table sets up columns as write enabled or read only
      nextTb.ReadOnly = nextCol.ReadOnly;
    
      // keep track of write enabled text boxes - SEE BELOW
      if (!nextCol.ReadOnly)
      {
        dataCols.Add(nextCol.Caption, nextCol);
        textBoxes.Add(nextCol.Caption, nextTb);
      }
    }
    

    您需要跟踪已启用写入的文本框并将它们与表格列相关联,以便在用户关闭对话框时更新数据行。您可以通过多种方式做到这一点。顺便说一句,这可以是两个字典,每个都由数据列标题键入。一个字典包含数据列,另一个包含相应的文本框。然后当用户关闭对话框时,您可以使用文本框值更新列值。

    Dictionary<String, DataColumn> dataCols;
    Dictionary<String, TextBox> textBoxes;
    foreach (String nextColName in dataCols.Keys)
      myRow[dataCols[nextColName]] = (Object)textBoxes[nextColName].Text;
    

    【讨论】:

      【解决方案2】:

      您可以使用 JQuery 数据表。它也是可编辑的。

      当 DataTables Editable 插件初始化时,它会向服务器端发送更新、删除和添加请求。 JQuery DataTables Editable 插件使用户能够选择和删除表中的行。当用户按下删除按钮时,将使用被删除记录的 id 信息发出 AJAX 请求。

      编辑单元格值

      编辑单元格是通过单击它来内联完成的。当用户完成编辑时,带有单元格位置和单元格值的 AJAX 请求会发送到服务器端。

      更新请求有以下参数:

      - 编辑后的文本

      id - 被编辑的记录的id(id被放置在已经被编辑的单元格周围的TR标签中)

      columnId - 已编辑单元格的列位置

      columnPosition - 已编辑单元格的列位置(不计算隐藏列)

      rowId - 包含已编辑单元格的行的 ID

      如果单元格在服务器端成功更新,服务器应返回单元格的新值,或者返回错误消息。

      初始化 jQueryData 表的代码

      您可以使用此代码

      <script language="javascript" type="text/javascript">
          $(document).ready(function () {
               $('#myDataTable').dataTable().makeEditable();
          });
      </script>
      

      【讨论】:

      • 这对于asp?亲爱的我用的是winform
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 2016-01-30
      • 2015-05-07
      • 2010-12-15
      • 2017-04-26
      相关资源
      最近更新 更多