How to copy a datagrid to an arraylist

Asked

Viewed 1,058 times

0

I have a button and command and when I click it, I need to copy all the information from my datagrid to an Arraylist. For example: The datagrid has 4 columns and 10 rows all have information, now comes my dilemma, when clicking on the batão need to copy all datragrid to an arrylista. I’m using the following Cód.

ArrayList Array = new ArrayList();    
foreach (DataGridViewCell item in dgvCompeticao.CurrentRow.Cells)
{
  Array.Add(item.Value);
}

But this Cód is only copying the line

  • Already tried to add columns in the code as well?

  • 1

    Do you want to copy all the lines to the list? Or do you want to copy all the cells from all the lines? If it is the second, how will the cells be organized inside the list? Do you have a list list? I warn you to use List in place of ArrayList. Not that it affects your problem, it affects all your problems.

4 answers

2

Put that code on your button and test; ( remembering that I put 04 columns in the DGV )

        for (int i = 0; i < 15; i++) //carrega os itens
        {
            dataGridView1.Rows.Add("Linha= " + i.ToString() + "\t Coluna=0 ",
                                   "Linha= " + i.ToString() + "\t Coluna=1 ",
                                   "Linha= " + i.ToString() + "\t Coluna=2 ",
                                   "Linha= " + i.ToString() + "\t Coluna=3 ");
        }

        string[,] myList = new string[dataGridView1.ColumnCount, dataGridView1.RowCount]; //define o tamanho da variavel

        for (int i = 0; i < dataGridView1.RowCount-1; i++) //percorre as linhas 
        {
            for (int x = 0; x < dataGridView1.ColumnCount - 1; x++) // percorre as colunas
            {
                myList[x, i] = dataGridView1[x, i].Value.ToString();
            }
        }

        MessageBox.Show(myList[2, 2]); //pega o resultado
  • You are creating a table in 1for?

  • Yes, to have a point where to start in the code.

  • This is just an example, I filled in the DGV, then created the variable, filled it in, then showed the result of the position I wanted by Msgbox, and YES, it was tested yes.

  • @Clevertoncarneiro I understood this, but what if you have empty cell? tostring(), do not accept and now? This is my Cap giving this exception msg: Object Reference not set to an instance of an Object.

  • @Clevertoncarneiro, has how to play this content in a variable type datetable?

1

Follow example with Datatable

    for (int i = 0; i < 15; i++)
    {
        dataGridView1.Rows.Add("Linha= " + i.ToString() + "\t Coluna=0 ",
                               "Linha= " + i.ToString() + "\t Coluna=1 ",
                               "Linha= " + i.ToString() + "\t Coluna=2 ",
                               "Linha= " + i.ToString() + "\t Coluna=3 ");
    }


    System.Data.DataTable dt = new System.Data.DataTable();

    //criação das colunas do datatable
    for (int x = 0; x < dataGridView1.ColumnCount; x++)
    {
        // o tipo de dados vc pode definir (int) (datetime) (decimal) e por ai vai
        dt.Columns.Add(dataGridView1.Columns[x].HeaderText, typeof(string)); 
    }

    //criação das colunas do datatable passagem do dados para o dataTable
    for (int i = 0; i < dataGridView1.RowCount - 1; i++)
    {
        dt.Rows.Add(""); //aqui vc cria a coluna
        for (int x = 0; x < dataGridView1.ColumnCount; x++)
        {
            //aqui vc atribuir o valor a coluna
            dt.Rows[i][x] = dataGridView1[x, i].Value.ToString();                    
        }
    }

    MessageBox.Show(dt.Rows[2][2].ToString());

0

You have to make an external loop that gets the lines, and a nested loop to get the cells from that line.

Similar to this here:

foreach (DataGridViewRow row in dgvCompeticao.Rows) {
  ArrayList array = new ArrayList();
  foreach (DataGridViewCell item in row.Cells) {
    array.Add(item.Value);
  }
}
  • Have you tested this? I don’t think it will work

  • I said similar. The idea is to scroll one line at a time, and then add the values of the cells in the array.

  • This one did not give friend @mutlei

0


My code went like this

string[,] myList = new string[dataGridView1.ColumnCount, dataGridView1.RowCount]; //define o tamanho da variavel

    for (int i = 0; i < dataGridView1.RowCount-1; i++) //percorre as linhas 
    {
        for (int x = 0; x < dataGridView1.ColumnCount - 1; x++) // percorre as colunas
        {
            if (dgvCompeticao[x, i].Value != null)
               {
                 myList[x, i] = dgvCompeticao[x, i].Value.ToString();
           }
        }
    }

So it worked out nice. Thank you very much.

Browser other questions tagged

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