Add data to a one column of a DataGrid C #

0

Good, I have problem when adding a data to a specific column of a datagrid in C #, I get an error that says:

foreach (double inter in intervalo)
{     
    if(nAle >= (double)intervalo[2*i] && nAle<=(double)intervalo[2*i-1])
    {
        i += 1;
        pos = intervalo.Count;
        table.Rows[i].Cells[i] += 1;
        break;
    }
}

    
asked by Luis Fernando Vargas 26.11.2017 в 19:56
source

2 answers

1

Good Luís,

The error you have, as the message indicates, is that the definition Cells does not exist for System.Data.DataRow .

To get a column of a Row specific within a DataTable you have to do it in the following way:

foreach (double inter in intervalo)
{     
    if(nAle >= (double)intervalo[2*i] && nAle<=(double)intervalo[2*i-1])
    {
        i += 1;
        pos = intervalo.Count;
        table.Rows[i][i] += 1; //Pongo i respetando tu código, el segundo sería el índice de la columna
        break;
    }
}

The table.Rows[i].Cells[i] works correctly when it comes to DataGridViewRow , but in DataRow is different, I recommend you look at the official Microsoft page and you are well informed of the differences of each class.

    
answered by 27.11.2017 в 08:31
-1

System.Data.DataRow does not contain a property named Cells . What you need is to access the index of the row using Rows[index] and then access the column of that row:

foreach (double inter in intervalo)
{     
    if(nAle >= (double)intervalo[2*i] && nAle<=(double)intervalo[2*i-1])
    {
        i += 1;
        pos = intervalo.Count;
        DataRow row = table.Rows[i];

        // row[i] retona un System.DataColumn por lo que le asignamos el valor a la columna con el indice i
        row[i] += 1 
        break;
    }
}
    
answered by 27.11.2017 в 14:08