我对 C# 非常陌生,如果这是一个新手问题,我很抱歉。
我有一个带有连接到 MySQL 数据库的数据网格的表单。
我已经做到了,因此它会选择整行,当您双击该行时,它会打开一个新表单。
我现在想做的是在第二个表单中添加一些文本框,其中填充用户在前一个表单中选择的数据。
这是我到目前为止的代码。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string connectionString = "datasource=*****;database=****;username=****;password=****;";
string query = "select firstname, surname, email from users;";
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = command;
DataTable dTable = new DataTable();
adapter.Fill(dTable);
dataGridView1.DataSource = dTable;
connection.Close();
}
private void viewData(object sender, DataGridViewCellEventArgs e)
{
Form2 secondForm = new Form2();
secondForm.Show();
}
}
最佳答案
您需要将数据表作为第二类的构造函数中的变量发送:
例如..在你的情况下:
public partial class Form1 : Form
{
public Form1()
{
//code that you have already wrote
Form2 f2 = new Form2(dTable);
f2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2(Datatable table)
{
//Do whatever you want with this table
//Example
label1.Text = table.Rows[0][0].ToString();
}
}
干杯!
关于c# 将数据从数据表传递到另一种形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28453871/