2015-10-20 96 views
-1

我需要在表格中顯示檢查結果,假設我對一個結果應該在第一行表格佈局面板中顯示的紙張進行了測試,並且if我採取一個更紙測試結果應該是在表佈局面板的第二行顯示動態使用C#(如果我在結果存儲按鈕點擊它應該重定向到測試結果表格並顯示結果)如何在窗體中動態地將數據插入表格

public partial class Form3 : Form 
{ 
    private string papId; 
    private string subject; 
    private string chapter; 
    private string tstname; 

    public Form3(string papId,string tech,string subject,string chapter, string tstname,int maxMarks,int marksObtained) 
    { 
     InitializeComponent(); 
     this.papId=papId; 
     this.subject=subject ; 
     this.chapter = chapter; 
     this.tstname = tstname; 
     int row = dsplyUserReslt.Count; 
     Label lblTech = getLabelDetails(); 
     lblTech.Text = tech; 
     dsplyUserReslt.Controls.Add(lblTech, 0, row-1); 
     Label lblSub = getLabelDetails(); 
     lblSub.Text = subject; 
     dsplyUserReslt.Controls.Add(lblSub,1, row-1); 
     Label lblChptr = getLabelDetails(); 
     lblChptr.Text = chapter; 
     dsplyUserReslt.Controls.Add(lblChptr, 2, row-1); 
     Label lbltstName = getLabelDetails(); 
     lbltstName.Text = tstname; 
     dsplyUserReslt.Controls.Add(lbltstName, 3, row-1); 
     Label lblmaxmarks = getLabelDetails(); 
     lblmaxmarks.Text = maxMarks.ToString(); 
     dsplyUserReslt.Controls.Add(lblmaxmarks, 4, row-1); 
     Label lblmarksobtnd = getLabelDetails(); 
     lblmarksobtnd.Text = marksObtained.ToString(); 
     dsplyUserReslt.Controls.Add(lblmarksobtnd, 5, row - 1); 
     Label date = getLabelDetails(); 
     date.Text = DateTime.Now.ToString("MMMM dd, yyyy"); 
     dsplyUserReslt.Controls.Add(date, 6, row - 1); 
     Button btnImproveScore = new Button(); 
     btnImproveScore.Text = "improve score"; 
     btnImproveScore.Size = new System.Drawing.Size(135, 25); 
     btnImproveScore.Font = new Font("Microsoft Sans Serif", 10); 
     dsplyUserReslt.Controls.Add(btnImproveScore, 7, row - 1); 
     btnImproveScore.Click+=new EventHandler(this.btnImproveScore_Click); 
     if (row > 4) 
     { dsplyUserReslt.RowCount = 4; } 
     else 
     { 
      dsplyUserReslt.RowCount = dsplyUserReslt.RowCount + 1; 
      dsplyUserReslt.RowStyles.Clear(); 
      dsplyUserReslt.RowStyles.Add(new RowStyle(SizeType.Absolute, 120F)); 
     } 


    } 
    public Label getLabelDetails() 
    { 
     Label lbl = new Label(); 
     lbl.AutoSize = true; 
     lbl.Font = new Font("Microsoft Sans Serif", 12); 
     lbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; 
     return lbl; 
    } 
+1

,並在你的代碼?你試過什麼了? – Marusyk

+1

SO不是一個軟件寫作服務......毫無疑問,這個問題將在適當的時候關閉。 –

+0

@MegaTron我把tabel佈局面板當作dsplyUsrReslt,並且最初它的行數是1.現在我的問題是如果我再次進行測試意味着它重新定向並且它正在顯示行計數值1 –

回答

0

使用DataGridView和DataTable

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     DataTable dt = new DataTable(); 
     public Form1() 
     { 
      InitializeComponent(); 


      dt.Columns.Add("ColA", typeof(int)); 
      dt.Columns.Add("ColB", typeof(string)); 

      dt.Rows.Add(new object[] { 1, "a" }); 
      dt.Rows.Add(new object[] { 2, "b" }); 
      dt.Rows.Add(new object[] { 3, "c" }); 
      dt.Rows.Add(new object[] { 4, "d" }); 
      dt.Rows.Add(new object[] { 5, "e" }); 

      dataGridView1.DataSource = dt; 

     } 
    } 
} 
​ 
​ 
+0

如果我把第二次測試意味着新的數據表將創建我不希望它應該只顯示在前面的表 –

+0

我將DataTable的定義移動到全局內存,所以你不要每次調用代碼時重新創建DataTable。 – jdweng

+0

存儲結果按鈕存在於一個表單中,當我點擊那個按鈕時,我將顯示結果表格放在下一個表格中,即使我們在全局存儲器中寫入數據表格也會執行 –

0

when wo使用多種形式,您必須使用表單的實例和輔助函數在表單之間移動數據。請參閱下面我的示例代碼

表1

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
    { 
     Form2 form2; 
     public Form1() 
     { 
      InitializeComponent(); 
      form2 = new Form2(this); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      form2.Show(); 
      string results = form2.GetData(); 
     } 
    } 
} 

表2

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication2 
{ 
    public partial class Form2 : Form 
    { 
     Form1 form1; 
     public Form2(Form1 nform1) 
     { 
      InitializeComponent(); 

      this.FormClosing += new FormClosingEventHandler(Form2_FormClosing); 
      form1 = nform1; 
      form1.Hide(); 
     } 
     private void Form2_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      //stops for from closing 
      e.Cancel = true; 
      this.Hide(); 
     } 
     public string GetData() 
     { 
      return "The quick brown fox jumped over the lazy dog"; 
     } 

    } 
} 
相關問題