简体   繁体   中英

Pass datagridview data to another datagridview in C#

So I am trying to pass data from DataGridView to another one using a CheckBox in a column to select the columns I want to transfer. But with the code that I already got I am able to pass on by one. I want to selected more that one and pass it to another DataGridView .

This is the code I have:

private void cmdCopy_Click(object sender, EventArgs e)
{
    frmRecycle frm = new frmRecycle();
    frm.dataGridView2.Rows.Clear();
    foreach (DataGridViewRow dtrow in dataGridView1.Rows)
    {
        if ((bool)dtrow.Cells[0].Value == true)
        {
            int n = frm.dataGridView2.Rows.Add();
            frm.dataGridView2.Rows[n].Cells[0].Value = true;
            frm.dataGridView2.Rows[n].Cells[1].Value = dtrow.Cells[1];
            frm.dataGridView2.Rows[n].Cells[2].Value = dtrow.Cells[2].Value.ToString();
            frm.dataGridView2.Rows[n].Cells[3].Value = dtrow.Cells[3].Value.ToString();
            frm.dataGridView2.Rows[n].Cells[4].Value = dtrow.Cells[4].Value.ToString();
            frm.dataGridView2.Rows[n].Cells[5].Value = dtrow.Cells[5].Value.ToString();
            frm.dataGridView2.Rows[n].Cells[6].Value = dtrow.Cells[6].Value.ToString();
            frm.dataGridView2.Rows[n].Cells[7].Value = dtrow.Cells[7].Value.ToString();
            frm.dataGridView2.Rows[n].Cells[8].Value = dtrow.Cells[8].Value.ToString();
            frm.dataGridView2.Rows[n].Cells[9].Value = dtrow.Cells[9].Value.ToString();

            con = new SqlConnection(cs.DBConn);
            con.Open();

            string queryInsert = @"INSERT INTO SentRecyle(FileName, TypeFile, SizeFile, PathFile,
                                   Created, Modified, Access, PcName)
                                   VALUES (@FileName, @TypeFile, @SizeFile, @PathFile,
                                   @Created, @Modified, @Access, @PcName)";
            cmd = new SqlCommand(queryInsert);
            cmd.Connection = con;

            cmd.Parameters.Add(new SqlParameter("@FileName", SqlDbType.NVarChar, 150, "FileName"));
            cmd.Parameters.Add(TypeFileSqlParameter("@TypeFile", SqlDbType.NVarChar, 150, "TypeFile"));
            cmd.Parameters.Add(new SqlParameter("@SizeFile", SqlDbType.Int, 20, "SizeFile"));
            cmd.Parameters.Add(new SqlParameter("@PathFile", SqlDbType.NVarChar, 150, "PathFile"));
            cmd.Parameters.Add(new SqlParameter("@Created", SqlDbType.NVarChar, 150, "Created"));
            cmd.Parameters.Add(new SqlParameter("@Modified", SqlDbType.NVarChar, 150, "Modified"));
            cmd.Parameters.Add(new SqlParameter("@Access", SqlDbType.NVarChar, 150, "Access"));
            cmd.Parameters.Add(new SqlParameter("@PcName", SqlDbType.NVarChar, 50, "PcName"));

            cmd.Parameters["@FileName"].Value = dtrow.Cells[2].Value.ToString();
            cmd.Parameters["@TypeFile"].Value = dtrow.Cells[3].Value.ToString();
            cmd.Parameters["@SizeFile"].Value = dtrow.Cells[4].Value.ToString();
            cmd.Parameters["@PathFile"].Value = dtrow.Cells[5].Value.ToString();
            cmd.Parameters["@Created"].Value = dtrow.Cells[6].Value.ToString();
            cmd.Parameters["@Modified"].Value = dtrow.Cells[7].Value.ToString();
            cmd.Parameters["@Access"].Value = dtrow.Cells[8].Value.ToString();
            cmd.Parameters["@PcName"].Value = dtrow.Cells[9].Value.ToString();

            cmd.ExecuteNonQuery();

            con.Close();

            con = new SqlConnection(cs.DBConn);
            con.Open();
            int AffectedRows = 0;
            string queryDelete = "DELETE FROM InfoFile WHERE FileName='" + dtrow.Cells[2].Value.ToString() + "'";
            cmd = new SqlCommand(queryDelete);
            cmd.Connection = con;

            AffectedRows = cmd.ExecuteNonQuery();

            if (AffectedRows > 0)
            {
                ShowDataGridView();
            }
            else
            {
                MostrarDataGridView();
                MessageBox.Show("There are no records!", "Try again", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }
}

The I have this on my DataGridView :

if ((bool)dataGridView1.SelectedRows[0].Cells[0].Value == false)
{
    dataGridView1.SelectedRows[0].Cells[0].Value = true;
}
else
{
    dataGridView1.SelectedRows[0].Cells[0].Value = false;
}

I would like to been able to pass more than one row. Do you guys have any idea how I can solve this?

When you say more than one row, I am assuming you're asking for more than the selected row, which would mean the datarows collection or the griddata object which is the datasource. I would copy either the collection or the datasource and pass that over.

So, you could pass the entire grid from the binding source like so

    private void PassCustomersDataGridView_DoubleClick(object sender, EventArgs e)
    {
        System.Data.DataRowView SelectedRowView; 
        NorthwindDataSet.CustomersRow SelectedRow;

        SelectedRowView = (System.Data.DataRowView)customersBindingSource.Current; // selected row
        SelectedRow = (NorthwindDataSet.CustomersRow)SelectedRowView.Row;

       // do something like this pass data source, 
       // and then convert it back to what you the correct type at the other side
        GridData = (System.Data)customersBindingSource; 

       // Form2 OrdersForm = new Form2();
       /// set your passed in data source here
       // OrdersForm.LoadOrders(SelectedRow.CustomerID);
       // OrdersForm.Show();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM