通过C#中的两个窗体表单在datagridview和textbox之间传递数据

时间:2016-05-11 18:44:26

标签: c# windows

我有Form2 DataGridView`,它由以下列组成:

  • ID
  • NAME
  • 简短说明
  • 说明

此外,我还有一个按钮,可以打开一个单独的 public class MyClassName { public string ID { get; set; } public string Name { get; set; } public string DESCRIPTION { get; set; } public string ShortDESCRIPTION { get; set; } } } ,其中包含多个文本框。

如何将数据从第二种形式传递到原始形式,Form2的textBox1中的值将传递给DataGridView的ID列? 它应该适用于所有列(textbox2到NAME..textbox4到DESCRIPTION)

public partial class Form2 : Form
    {

public  MyClassName mc;
public Form2() 
{      
           InitializeComponent(); 
} // save text of your textboxs to your property public void.       
public void SaveTextBoxesToMyObjectOfClass() 
{ 
     mc = new MyClassName();
     mc.ID = textBox1.Text; 
} 
private void Form2_Load(object sender, EventArgs e) 
{  
} 
private void textBox1_TextChanged(object sender, EventArgs e) 
{ 

} 
private void button1_Click(object sender, EventArgs e) 
{
    SaveTextBoxesToMyObjectOfClass();
    Close();
} 
}
}

// Form2

      public partial class Form1 : Form
    {


    public Form1()
    {
         InitializeComponent();
         this.Load+=new EventHandler(Form1_Load);
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        MyClassName mc = new MyClassName();
        Form2 f = new Form2();
        f.ShowDialog();
        this.dataGridView1.DataSource = mc;
    }

} 

\ Form1中

  count(case
           when 
           TO_CHAR(emp_expected_date , 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('SAT', 'SUN') 
           and TO_CHAR(emp_actual_date , 'DY', 'NLS_DATE_LANGUAGE=ENGLISH')
           NOT IN ('SAT', 'SUN') THEN emp_id OVER (PARTITION BY contractor)  ELSE NULL end)
           AS contractor_non_compliance

如果你的意思是这样,它就不会传递数据。 你能纠正我吗?

1 个答案:

答案 0 :(得分:1)

你在课堂上得到了Form1的代码。将其移至方法或事件处理程序,如Form_Load或表单构造函数。

public partial class Form1 : Form 
{ 
    public Form1()
    {
         InitializeComponent();
         this.Load+=new EventHandler(Form1_Load);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Form2 f= new Form2();
        f.ShowDialog();
        MyClass mc = f.mc;
    }

} 

修改 这就是您的Form2应该是这样的。注意mc是在类级别定义的,因此可以从表单外部访问:

MyClassName mc;
public Form2() 
{      
           InitializeComponent(); 
} // save text of your textboxs to your property public void.       
SaveTextBoxesToMyObjectOfClass() 
{ 
     mc = new MyClassName();
     mc.ID = textBox1.Text; 
} 
private void Form2_Load(object sender, EventArgs e) 
{  
} 
private void textBox1_TextChanged(object sender, EventArgs e) 
{ 

} 
private void button1_Click(object sender, EventArgs e) 
{
    SaveTextBoxesToMyObjectOfClass();
    Close();
} 

这会在按钮点击时保存您的对象并在那一刻关闭表单