C# 将DGV选定行传递到第二个表单

C# 将DGV选定行传递到第二个表单,c#,winforms,datagridview,C#,Winforms,Datagridview,我正在尝试将datagridview的选定行传递给第二个表单。然后选择第二个DGV中的项目,并在该点将信息传递到表中。我要做的是按以下方式传递行: 表格1: 在表格2中,我似乎无法访问DGW。不确定这是正确的方法还是有更好的方法 表格2: public frmDupeAdd(DataGridViewSelectedRowCollection selectedRows) { InitializeComponent(); DataGr

我正在尝试将datagridview的选定行传递给第二个表单。然后选择第二个DGV中的项目,并在该点将信息传递到表中。我要做的是按以下方式传递行: 表格1:

在表格2中,我似乎无法访问DGW。不确定这是正确的方法还是有更好的方法

表格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();
                }


            }


        }
而不是

public frmDupeAdd(DataGridViewSelectedRowCollection selectedRows)
 {
            InitializeComponent();
            DataGridViewSelectedRowCollection dgIntRows = selectedRows;
 }
使用


您正在构造函数中声明
dgintows
。当构造函数完成时,该变量将离开作用域。将其声明为类中的
私有
字段,并在构造函数中分配给它,其他方法可以访问它。从技术上讲,更吸引人的方法是避免传递表单特定的数据,而是让两个表单都使用数据提供程序。然后它可以用来传递甚至自动同步两种形式的项目选择。@Sinatr这是什么意思?两种表单都使用数据提供程序。我所拥有的是一个带有DGV的表单,用户将地址添加到其中,我希望能够选择一个或多个地址,点击按钮并从该案例中选择其他兴趣,然后将所选地址添加到这些选定兴趣中。随后将进行后续操作,允许用户键入兴趣名称,查找与该名称关联的地址,并将这些地址添加到当前记录/兴趣中。
public frmDupeAdd(DataGridViewSelectedRowCollection selectedRows)
 {
            InitializeComponent();
            DataGridViewSelectedRowCollection dgIntRows = selectedRows;
 }
private DataGridViewSelectedRowCollection dgIntRows = new DataGridViewSelectedRowCollection ()
    public frmDupeAdd(DataGridViewSelectedRowCollection selectedRows)
         {
                    InitializeComponent();
                    dgIntRows = selectedRows;
         }