How to copy a datagrid to an arraylist

0

I have a button and command and when I click on it, I need to copy all information from my datagrid to an Arraylist. For example: The datagrid has 4 columns and 10 lines all have information, now comes my dilemma, when clicking on the baton I need to copy all datragrid to an arrylista. I am using the following code.

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

But this code is copying just the line

    
asked by anonymous 19.03.2015 / 15:59

4 answers

0

My code stayed 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 cool. Many thanks.

    
19.03.2015 / 20:26
2

Place this code on your button and test; (remembering that I put 04 columns in 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
    
19.03.2015 / 17:27
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());
    
19.03.2015 / 20:07
0

You have to do an external loop that gets the lines, and a nested loop to get the cells of 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);
  }
}
    
19.03.2015 / 16:02