将数据从一个表单传递到另一个表单
本文关键字:表单 另一个 一个 数据 | 更新日期: 2023-09-27 18:04:28
我有一个关于从一个表单传递数据到另一个表单的问题。
这是我在学校的作业。在我的第一个形式,我有一个按钮和3个文本框,其中标有id,名称和地址。当我点击按钮另一个形式将出现,我也希望第一个形式是不可访问的。第二种形式显示了数据列表(id,名称,地址),这是从我已经创建的数据库。选择所需数据后,有一个标有"选择"的按钮,代码如下:
private void metroButtonSelect_Click(object sender, EventArgs e)
{
string selectedwcNo = metroGrid1.SelectedRows[0].Cells[0].Value.ToString();
string selectedwcFullname = metroGrid1.SelectedRows[0].Cells[1].Value.ToString();
string selectedwcAddress = metroGrid1.SelectedRows[0].Cells[2].Value.ToString();
}
我不知道下一步该做什么。我想把这些数据传递给第一个表单
为您的值创建一个类:
public class Person
{
public string No {get; set;}
public string Name {get; set;}
public string Address {get; set;}
}
在你的第二个表单中添加readonly
公共属性,从那里你得到选择的数据。
public class SecondForm
{
private Person _SelectedPerson;
public Person SelectedPerson
{
get { return _SelectedPerson; }
}
//Set data to the SelectedPerson in your click eventhandler
private void metroButtonSelect_Click(object sender, EventArgs e)
{
_SelectedPerson = new Person();
_SelectedPerson.No = metroGrid1.SelectedRows[0].Cells[0].Value.ToString();
_SelectedPerson.Name = metroGrid1.SelectedRows[0].Cells[1].Value.ToString();
_SelectedPerson.Address = metroGrid1.SelectedRows[0].Cells[2].Value.ToString();
}
}
然后在你的第一个方法创建方法打开第二个窗体并返回选定的数据
public class FirstForm
{
private Person GetSelectedPerson();
{
Person selected = null;
using(var secondForm = new SecondForm())
{
secondForm.ShowDialog();
selected = secondForm.SelectedPerson;
}
return selected;
}
//And use above method in button click eventhandler
private void Button_Click(object sender, EventArgs e)
{
Person selected = this.GetSelectedPerson();
if(selected != null)
{
//Show selected data in the textboxes
this.TextBoxNo.Text = selected.No;
this.TextBoxName.Text = selected.Name;
this.TextBoxAddress.Text = selected.Address;
}
}
}
secondForm.ShowDialog()
方法将form显示为modal,这将使第一个form不可访问。
创建新表单:
Form2 frm2 = new Form2();
frm2.show();
隐藏表单:
this.Hide();
我真的不知道你到底想做什么,但也许这会有所帮助https://www.youtube.com/watch?v=PI3ad-TebP0 当DataGridView用于在两个表单上显示/更改相同的数据时,另一个特性可以更可取- 数据绑定。如果第二个表单的数据在第一个表单上发生了变化,它将自动更新。
下面的例子演示了如何使用它。
Persons.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace FormsDataGridViewBinding
{
public class Person : INotifyPropertyChanged
{
public Person(string n, string name, string address)
{
_number = n;
_name = name;
_address = address;
}
public string Number
{
get { return _number; }
set
{
if (_number != value)
{
_number = value;
this.NotifyPropertyChanged("Number");
}
}
}
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
this.NotifyPropertyChanged("Name");
}
}
}
public string Address
{
get { return _address; }
set
{
if (_address != value)
{
_address = value;
this.NotifyPropertyChanged("Address");
}
}
}
private string _number;
private string _name;
private string _address;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
Form1.cs
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
namespace FormsDataGridViewBinding
{
public partial class Form1 : Form
{
BindingList<Person> personList = new BindingList<Person>();
public Form1()
{
personList.Add(new Person("number1", "name1", "address1"));
personList.Add(new Person("number2", "name2", "address2"));
InitializeComponent();
dataGridView1.DataSource = personList;
}
private void button1_Click(object sender, System.EventArgs e)
{
using (var form2 = new Form2())
{
form2.Persons = personList;
form2.ShowDialog();
}
}
}
}
Form2.cs
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
namespace FormsDataGridViewBinding
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public BindingList<Person> Persons
{
get; set;
}
private void Form2_Load(object sender, System.EventArgs e)
{
dataGridView1.DataSource = Persons;
}
}
}