【发布时间】:2012-05-17 12:23:21
【问题描述】:
我有一个程序,其中包含许多“创建新 [xxx]”表单(包含文本框、组合框、日期时间 picekrs 等),这些表单稍后会保存到数据库中。为了查看该数据,我使用了相应的表单“view [xxx]”,其中包含从数据库填充的 datagridview。
当用户点击数据网格视图中的字段时,数据应该被发送回原来的“创建”表单。
到目前为止,我一直通过从 datagridview 传递整行数据来重载“创建”表单的构造函数:
示例:
查看表格:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1)
{
ID = dataGridView1.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
string posName = dataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue.ToString();
string posDesc = dataGridView1.Rows[e.RowIndex].Cells[2].FormattedValue.ToString();
frmNewStaffPosition frm = new frmNewStaffPosition(posName, posDesc);
frm.Show();
frm.NewTypePositionAdded += new frmNewStaffPosition.AddNewTypeOfStaffPosition(frm_NewTypePositionAdded);
创建表格:
public frmNewStaffPosition( string pos, string desc)
{
InitializeComponent();
tbPositionName.Text = pos;
tbNewStaffPositionDecription.Text = desc;
}
虽然这可行,但我不认为它是最好的解决方案,尤其是当从 VIEW 传递的数据包含许多列时。有没有更优雅的解决方案,即只传递选定行的 PK 并直接从数据库填写 CREATE 表单?
如果没关系,我通过 MySQL 连接器使用 MySQL 数据库。
【问题讨论】:
-
你找到最好的方法了吗?
标签: c# mysql visual-studio-2010 datagridview