将DGV选定的行传递给第二种形式

时间:2014-10-30 12:37:19

标签: c# winforms datagridview

我正在尝试将数据网格视图的选定行传递给第二种形式。然后选择第二个DGV中的项目,并将信息传递到该点的表格中。我试图做的是以下面的方式传递行: Form1中:

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


            }


        }

1 个答案:

答案 0 :(得分:0)

而不是

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

使用

private DataGridViewSelectedRowCollection dgIntRows = new DataGridViewSelectedRowCollection ()
    public frmDupeAdd(DataGridViewSelectedRowCollection selectedRows)
         {
                    InitializeComponent();
                    dgIntRows = selectedRows;
         }