2015-10-20 100 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"; 
     } 

    } 
} 
相关问题