我已经详尽地研究过(谷歌搜索)这个问题的解决方案,但却找不到。我的问题是,在Form1中选择一行(为清楚起见,称为“行”),它不会将值传输到Form2中的DataGridView
。请注意,所有列标题都已传输。
以下是Form1的片段:
private void customersDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
OrderSearchForm orderform = new OrderSearchForm();
orderform.Row = this.customersDataGridView.CurrentRow;
orderform.MdiParent = this.MdiParent;
orderform.Show();
}
Form2:
private DataGridViewRow row;
public DataGridViewRow Row
{
get { return row; }
set { row = value; }
}
private void OrderSearchForm_Load(object sender, EventArgs e)
{
NorthwindDataClassesDataContext db = new NorthwindDataClassesDataContext();
DataGridViewRow r = row.Clone() as DataGridViewRow;
foreach (DataGridViewCell cell in row.Cells)
{
this.OrderSearchgridview.Columns.Add(cell.OwningColumn.Name,
cell.OwningColumn.HeaderText);
r.Cells[cell.ColumnIndex].Value = cell.Value;
}
OrderSearchgridview.Rows.Add(r);
}
基本上,我的问题是,如何在Form2的DataGridView
中显示Form1的DataGridView
中的行值?
我觉得我在插入列后错过了添加实际值的步骤,但此时我仍处于困境。
答案 0 :(得分:0)
最好还有一个用于OrderSearchForm的Contrucutor来保存Row的值。
考虑Row有3个值,如ID,Name,Age
使用这些Pramaeteter为OrderSearchForm创建对象。
OrderSearchForm orderform = new OrderSearchForm(New User(customersDataGridView.SelectedRows[0].Cells[0].Value.ToString(),SelectedRows[0].Cells[1].Value.ToString(),SelectedRows[0].Cells[2].Value.ToString()));
orderform.MdiParent = this.MdiParent;
orderform.Show();
Form2:
private DataGridViewRow row;
private User userObj;
public OrderSearchForm(User user)
{
this.userObj=user;
}
public DataGridViewRow Row
{
get { return row; }
set { row = value; }
}
private void OrderSearchForm_Load(object sender, EventArgs e)
{
//Do Operation with User's Properties.as you Like
OrderSearchgridview.Rows.Add(r);
}