0

可能是一个复杂的问题 - 但这里有希望

只需通过交换数据集中的表,我就可以在单个表单上拥有一个通用数据网格(显示我想要从数据集中获得的任何表)。

我想双击给定记录并显示表数据的单记录视图,当前选择的记录显示为默认值,但可以选择翻页和编辑/查看/删除其他记录,即

我想在运行时从给定表的数据网格中自动创建详细信息视图表单。应该动态创建表单 - 在详细信息视图中显示数据集,并可选择使用绑定源/绑定源导航器对单个记录进行分页。

我的目标是提高应用程序的效率/可维护性 - 而不是创建和管理 10 多个表单,我只想以与管理通用 gridview 表单相同的方式创建和管理通用详细信息表单。

到目前为止,我想出了:

public void CreateDetailsForm(BindingSource bs, int rowClicked) { Form detailsForm = new Form();

        BindingSource newFormBS = new BindingSource();

        //assign binding source for use on new detailsForm
        newFormBS = bs;

        //assign binding source to datatable
        DataTable dt = (DataTable)bs.DataSource;

        //create the form fields
        CreateFormFields(dt);  //not yet implemented

        //assign form fields to form

       //display form

}

对以下内容的任何帮助表示赞赏

  1. 生成表单字段并将其分配给表单。

提前致谢。

4

3 回答 3

0

它喜欢:

Form f=new Form();

        TextBox t=new TextBox();//generate the controls u need
        t.Text = "test";//set the actual value
        t.Location=new Point(10,10);
        f.Controls.Add(t);

        DialogResult dr=f.ShowDialog();
于 2013-05-09T06:17:41.030 回答
0

好的 - 现在可以工作了 - 必须采取不同的方法

  1. 创建了一个 detailsView 表单
  2. 创建了一个名为 PassBindingSource 的静态类,具有静态属性 bst 用于将绑定源从 gridview 传递到详细信息表单

    静态类 PassBindingSource { 公共静态 BindingSource bst { 获取;放; } }

  3. 在 detailsView 表单上添加了以下代码

    try{  bs.DataSource = PassBindingSource.bst;
    
    DataTable dt = (DataTable)PassBindingSource.bst.DataSource;
    
    
    List<string> colNames = GetColumnNames(dt);
    
    int offset = 25;
    int xPos = 50;
    int yPos = 50;
    
    foreach (string name in colNames)
     {
    Label l = new Label();
     l.Name = name;
    l.Width = 200;
    l.Text = name;
    
    
    TextBox t = new TextBox();
     t.Name = name;
    t.Width = 200;
    
     // BindingOperations.SetBinding(t, t.TextProperty, bs);
    
    //binding operation
    t.DataBindings.Add(new Binding("Text", bs, t.Name, true));
    
    l.Location = new Point(xPos, yPos);
    t.Location = new Point(xPos + 250, yPos);
    
    
    this.Controls.Add(l);
    this.Controls.Add(t);
    
    yPos = yPos + offset;
    
    // dgDetailsView.DataSource = bs;
     }
    //move to correct record in binding source
            bs.Position = PassBindingSource.currentRecord;
    }
    catch (Exception ex)
     {
     MessageBox.Show(ex.Message);
    }
    
    
    
    }
    
    
    
    private List<string> GetColumnNames(DataTable table)
    {
    List<string> lstColNames=new List<string>();
    
    if (table != null)
    {
    
          lstColNames=
    
          (from DataColumn col in table.Columns
    
           select col.ColumnName).ToList();
    
    
    }
    
    return lstColNames;
    
    
    
    }
    

总结 现在一切正常 - 在运行时生成的 detailsView 控件正确连接到绑定源,并且数据网格可以通过使用以下代码连接 gridview 的双击事件,随时使用数据集中的任何表调用此 detailsView

PassBindingSource.bst= bs;
frmDetailsView nf = new frmDetailsView();
nf.Show();

希望这对其他人有帮助。非常感谢 User2354374 的初始指导。

于 2013-05-09T09:14:40.363 回答
0

到目前为止,我已经在表单上生成了 col 名称,如下所示

列出 colNames = GetColumnNames(dt);

        int offset=25;
        int xPos=50;
        int yPos = 10;
        foreach (string name in colNames)
        {
            Label l = new Label();
            l.Name = name;
            l.Width = 200;
            l.Text = name;


            TextBox t = new TextBox();
            t.Name = name; 
            t.Width=200;

            l.Location = new Point(xPos, yPos );
            t.Location = new Point(xPos+250, yPos);


           f.Controls.Add(l);
           f.Controls.Add(t);

           yPos = yPos + offset;
        }
        //TextBox t = new TextBox();//generate the controls u need
        //t.Text = "test";//set the actual value


        f.Width = 800;
        f.Height = 600;
        f.Show();


    }

    private List<string> GetColumnNames(DataTable table)
    {
        List<string> lstColNames=new List<string>();

        if (table != null)
        {

             lstColNames=

                (from DataColumn col in table.Columns

                 select col.ColumnName).ToList();



        }

        return lstColNames;



    }

现在尝试让控件绑定到绑定源!

于 2013-05-09T06:40:31.707 回答