How to copy data from one gridview to the other?

Asked

Viewed 78 times

0

how do I copy data from one grid to another added a blank line every two copied data lines?

    TAB_F_TABELAS_DAL d = new TAB_F_TABELAS_DAL();
    var dados = d.RetornaCronograma(anotual+mesatual,anoanterior+mesanterior);
    CT01_Array.DataSource = dados;
    CT01_Array.DataBind();
  • 1

    Why do you want to do it? That? Isn’t it better to settle with an Itemtemplate? Is it Windowsform or Webform? Include the Gridview Markup

  • It’s Webform, I’ll test it here

1 answer

2


Please Rafael Veloso, you can analyze the code below?

 private void btnCopiar_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.TableName = "MinhaTabela";
        int intConta = 1;

        //Copia as colunas
        foreach (DataGridViewColumn col in dataGridViewOrigem.Columns)
            dt.Columns.Add(col.DataPropertyName, col.ValueType);

        //Copia as linhas
        foreach (DataGridViewRow gridRow in dataGridViewOrigem.Rows)
        {
            if (gridRow.IsNewRow)
                continue;

            //Se for divisível por "3" adiciona uma linha em branco
            if (intConta % 3 == 0)
                dt.Rows.Add();

            intConta++;

            DataRow dtRow = dt.NewRow();

            //Copia cada célula da linha
            for (int i1 = 0; i1 < dataGridViewOrigem.Columns.Count; i1++)
                dtRow[i1] = (gridRow.Cells[i1].Value == null ? DBNull.Value : gridRow.Cells[i1].Value);

            dt.Rows.Add(dtRow);
        }

        ds.Tables.Add(dt);
        this.dataGridViewCopia.DataSource = ds.Tables["MinhaTabela"];
    }
  • I just didn’t understand this dataGridViewSefaz, it is referring to what?

  • Oops... this datagrid is mine! You need to switch to the name of your source datagrid. The destination datagrid is in "dataGridViewCopia". I will change the name to "dataGridViewOrigem".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.