1

I have 2 GridView controls. I need to add the selected row from one GridView to a second GridView. (I select row from GridView by clicking direct to GridView.)

Here's my code, but it copies all data from gridview1 to gridview2. I need the selected row only.

private void button6_Click(object sender, EventArgs e)
    {
       DataGridViewColumn newCol = null;
        foreach (DataGridViewColumn col in dataGridView1.Columns)
        {
            newCol = new DataGridViewColumn(col.CellTemplate);
            newCol.HeaderText = col.HeaderText;
            newCol.Name = col.Name;
            dataGridView2.Columns.Add(newCol);
        }

        dataGridView2.RowCount = dataGridView1.RowCount;
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            foreach (DataGridViewColumn col in dataGridView1.Columns)
            {
                dataGridView2.Rows[row.Index].Cells[col.Name].Value = row.Cells[col.Name].Value;
            }
        }

I populated dataGridView1 with a DataTable:

SqlConnection con = new SqlConnection(@"Data Source=AFZAL\SQLEXPRESS;Initial Catalog=GIMS_LabInfo;Integrated Security=True");
con.Open();
SqlCommand sc = new SqlCommand("SELECT PROFCODE,PROFNAME FROM PROFNAMES$ WHERE (PROFNAME LIKE '" + textBox1.Text + "%') AND PROFCODE NOT IN (SELECT PROFCODE FROM MAP) ORDER BY Profname desc ", con);
sc.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(sc);
DataTable data = new DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Afzal Qazi
  • 15
  • 1
  • 6
  • 1
    Can you please provide some of your existing code? – Jake Chasan Sep 01 '14 at 15:23
  • Please see above my code. – Afzal Qazi Sep 01 '14 at 15:31
  • it copies all data from grid view 1 to grid view 2 but I only need the selected row. – Afzal Qazi Sep 01 '14 at 15:32
  • SqlConnection con = new SqlConnection(@"Data Source=AFZAL\SQLEXPRESS;Initial Catalog=GIMS_LabInfo;Integrated Security=True"); con.Open(); SqlCommand sc = new SqlCommand("SELECT PROFCODE,PROFNAME FROM PROFNAMES$ WHERE (PROFNAME LIKE '" + textBox1.Text + "%') AND PROFCODE NOT IN (SELECT PROFCODE FROM MAP) ORDER BY Profname desc ", con); sc.ExecuteNonQuery(); SqlDataAdapter sda = new SqlDataAdapter(sc); DataTable data = new DataTable(); sda.Fill(data); dataGridView1.DataSource = data; – Afzal Qazi Sep 01 '14 at 16:20
  • I populated dataGridView1 with dataTable. – Afzal Qazi Sep 01 '14 at 16:22

1 Answers1

0

Immediately after setting dataGridView1.DataSource:

dataGridView1.DataSource = yourDataTable;

Set dataGridView2.DataSource to clone the structure (such as Columns) of the first DataTable:

dataGridView2.DataSource = yourDataTable.Clone();

Or:

dataGridView2.DataSource = ((DataTable)dataGridView1.DataSource).Clone();

Then in your button click event, all you need is this:

private void button6_Click(object sender, EventArgs e)
{
    if (dataGridView1.CurrentRow == null)
        return;

    var currentRow = ((DataRowView)dataGridView1.CurrentRow.DataBoundItem).Row;

    ((DataTable)dataGridView2.DataSource).ImportRow(currentRow);
}

How can I add multiple rows on each click? As right now when I select second row, it replaces the First one in dataGridView2.

If you have multiple selected rows, you'll have to iterate over the SelectedRows property. Try this:

private void button1_Click(object sender, EventArgs e)
{
    if (dataGridView1.CurrentRow == null)
        return;

    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        ((DataTable)dataGridView2.DataSource).ImportRow(((DataRowView)row.DataBoundItem).Row);
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Thank you so much. it's working now. Could you guide me, How can I add multiple rows on each click. As right now when I select second row, it replaces the First one in dataGridView2. Each time when I click on button it should add on the records in dataGridView2. Thanks in advance. – Afzal Qazi Sep 02 '14 at 08:25
  • Dear Grant Winney, Thank you very much for your help but I am still not getting my desired output. What I really want is: When I select Row(s) in dataGridview1 and press button, selected Row(s) should be added in dataGridview2. Above code is doing so. But when I select next Row(s) and press button again, it should be addition in the existing rows in dataGridview2 (should not replace previously added Row(s)). In simple words, Row(s) should be added one by one on each button press. Please help. I appreciate your efforts. :) – Afzal Qazi Sep 03 '14 at 22:46
  • It does replaces existing Row(s) in dataGridView2. :( – Afzal Qazi Sep 04 '14 at 12:31