开发者

Copying rows from one DataGridView to another

开发者 https://www.devze.com 2023-03-24 07:36 出处:网络
I have written an application that is connected to SQL database in C#.There are two forms. I have a DataGrid in the first form. There are those columns like ID,AD,SOYAD in Datagridview.

I have written an application that is connected to SQL database in C#.There are two forms.

I have a DataGrid in the first form. There are those columns like ID,AD,SOYAD in Datagridview.

I have a DataGrid in the second form(frm4) and there are those columns like ID,AD,SOYAD in Datagridview.

I put ContextMenuStrip in the first DataGridView.

My question is:I want to add to second DataGridView those rows which are choosen in the first DataGridView.

  frm4.dataGridView1.Rows[0].Cells[0].Value = dataGridView1.CurrentRow.Cells[0].Value.ToString();
  frm4.dataGridView1.Rows[0].Cells[1].Value = dataGridView1.CurrentRow.Cells[1].Value.ToString();
  frm4.dataGridView1.Rows[0].Cells[2].Value = dataGridView1.CurrentRow.Cells[2].Value.ToString();

I only can add one row with upper code. But,I want to add multiple rows. I'm using below code for that. But, it's not working.

  for (int i = 0; i < length; i++)
  {
      frm4.dataGridView1.Rows[0].Cells[0].Value = da开发者_JS百科taGridView1.CurrentRow.Cells[0].Value.ToString();
      frm4.dataGridView1.Rows[0].Cells[1].Value = dataGridView1.CurrentRow.Cells[1].Value.ToString();
      frm4.dataGridView1.Rows[0].Cells[2].Value = dataGridView1.CurrentRow.Cells[2].Value.ToString();
  }


The following code works, though it can probably be improved. Also, you might want to take a step back and instead look at your datasource for the grid. If you are using a binding source for example you should be able to copy that and create the source for the second grid from there.

//to copy the rows you need to have created the columns:
foreach (DataGridViewColumn c in dataGridView1.Columns)
{                
    dataGridView2.Columns.Add(c.Clone() as DataGridViewColumn);
}

//then you can copy the rows values one by one (working on the selectedrows collection)
foreach (DataGridViewRow r in dataGridView1.SelectedRows)
{
    int index = dataGridView2.Rows.Add(r.Clone() as DataGridViewRow);
    foreach (DataGridViewCell o in r.Cells)
    {
        dataGridView2.Rows[index].Cells[o.ColumnIndex].Value = o.Value;
    }            
}


assume this is windows form application

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (this.dataGridView2.DataSource != null)
    {
        this.dataGridView2.DataSource = null;
    }
    else
    {
        this.dataGridView2.Rows.Clear();
    }
    for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
    {
        int index = dataGridView2.Rows.Add();
        dataGridView2.Rows[index].Cells[0].Value = dataGridView1.SelectedRows[i].Cells[0].Value.ToString();
        dataGridView2.Rows[index].Cells[1].Value = dataGridView1.SelectedRows[i].Cells[1].Value.ToString();
        .....
    }
}


Try using the Add method of the DataGridView. At this moment you're overwriting whatever value is on the first row. By using the Add method you add an extra row to the DataGridView.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号