【发布时间】:2014-11-01 18:24:16
【问题描述】:
我正在尝试将 datagridview 的选定行传递给第二个表单。然后在第二个 DGV 中选择项目并将信息传递到该点的表中。我试图做的是按以下方式传递行: 表格1:
private void toolStripLabel1_Click(object sender, EventArgs e)
{
int interestsKey;
interestsKey = Convert.ToInt32(interestsKeyTextBox.Text);
DataGridViewSelectedRowCollection rows = interestAddDataGridView.SelectedRows;
frmDupeAdd AddDupeForm = new frmDupeAdd(interestAddDataGridView.SelectedRows);
AddDupeForm.Show();
}
在表格 2 上,我似乎无法访问 dgIntRows。不确定这是否是正确的方法,或者是否有更好的方法。
表格2:
public frmDupeAdd(DataGridViewSelectedRowCollection selectedRows)
{
InitializeComponent();
DataGridViewSelectedRowCollection dgIntRows = selectedRows;
}
private void btnAddAdds_Click(object sender, EventArgs e)
{
DataGridViewSelectedRowCollection rows = dgCaseInt.SelectedRows;
dgCaseInt.Columns[4].Visible = false;
dgCaseInt.Columns[5].Visible = false;
foreach (DataGridViewRow row in rows)
{
DataClasses1DataContext db = new DataClasses1DataContext();
int interestkeyint = Convert.ToInt16(row.Cells[4].Value);
InterestAdd newinterestadd = new InterestAdd();
newinterestadd.InterestsKey = interestkeyint;
foreach (DataGridViewRow row in dgIntRows)
{
newinterestadd.CaseNumberKey = (string)row.Cells[5].Value; ;
newinterestadd.streetNum = (string)row.Cells[2].Value;
newinterestadd.Direction = (string)row.Cells[2].Value;
newinterestadd.Add1 = (string)row.Cells[3].Value;
newinterestadd.Suffix = (string)row.Cells[4].Value;
newinterestadd.Unit = (string)row.Cells[5].Value;
newinterestadd.City = (string)row.Cells[6].Value;
newinterestadd.State = (string)row.Cells[7].Value;
newinterestadd.Zip = (string)row.Cells[8].Value;
db.InterestAdds.InsertOnSubmit(newinterestadd);
db.SubmitChanges();
}
}
}
【问题讨论】:
-
你在构造函数中声明了
dgIntRows。当构造函数完成该变量离开范围。在你的类中声明为private字段,并在构造函数中赋值给它,其他方法可以访问。 -
技术上更吸引人的方法是避免传递特定于表单的数据,但要有两种表单都使用的数据提供程序。然后可以使用它来传递甚至自动同步两种表单中的项目选择。
-
@Sinatr 你这是什么意思?两种形式都使用数据提供者。我所拥有的是一个带有 DGV 的表单,用户向其中添加地址,我希望能够选择一个或多个这些地址,点击按钮并从该案例中选择其他兴趣,并将选择的地址添加到选择的地址利益。然后会有后续跟进,允许用户输入兴趣名称,找到与该名称关联的地址并将这些地址添加到当前记录/兴趣中。
标签: c# winforms datagridview