2013-05-09 55 views
0

可能創建一個詳細表單一個複雜的問題 - 但這裏的希望從一個GridView C#

我有一個單一的形式(顯示器無論表我從一個數據集要)上的通用數據網格只需在數據集中交換表。

我想雙擊給定的記錄,並顯示錶格數據的單記錄視圖,當前選定的記錄顯示爲默認值,但可以選擇翻頁和編輯/查看/刪除其他記錄,即

我想在運行時從給定表格的數據網格自動創建詳細信息視圖表單。應該動態創建表單 - 在詳細信息視圖中顯示數據集,並使用綁定源/綁定源導航器來選擇單頁記錄。

我的目標是提高應用程序的效率/可維護性 - 而不是創建和管理10多個表單,我只是想創建和管理我的通用細節表單,就像我管理通用GridView表單一樣。

到目前爲止,我想出了:

公共無效CreateDetailsForm(BindingSource的BS,INT rowClicked) { 形式detailsForm =新表();

 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. 生成並分配所述表單字段的表單

    }

    任何幫助。

在此先感謝。

回答

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(); 
+0

謝謝 - 已經使用此代碼生成帶有文本框和標籤的表單 - 現在我需要做的就是解決如何將表單上的控件綁定到綁定源。 – user2354374 2013-05-09 06:38:41

+0

不客氣,我想你可以生成一個datagridview或者其他東西來控制顯示你的數據源 – Nate 2013-05-09 06:53:10

0

到目前爲止,我如下

列表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; 



    } 

現在試圖讓控件綁定到綁定源!

+0

現在已經完成了單個記錄的綁定操作 - 但是可以在整個表上使用//綁定操作 t.DataBindings.Add (new Binding(「Text」,bs,t.Name,true)); – user2354374 2013-05-09 07:21:28

0

OK - 得到它現在的工作 - 不得不採取不同的方法

  1. 創建一個DetailsView控件形式
  2. 創建了一個名爲PassBindingSource與靜態屬性BST靜態類從GridView控件通過綁定源詳細信息表格

    static class PassBindingSource { public static BindingSource bst {get;組; } }

  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控件正確接線到綁定源並且datagrid可以隨時使用數據集中的任何表格通過將gridview的雙擊事件與以下代碼進行連接來調用此detailsView

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

希望這可以幫助別人。非常感謝User2354374的初始轉向。