I want to save GridView cells in a list to compare it with another list.
This is what I tried:
BudgetCommessa budgetCommessa = new BudgetCommessa();
List<BudgetCommessa> listaDaGridView = new List<BudgetCommessa>();
for (int i = 1; i <= preventivoView.Rows.Count; i++)
{
budgetCommessa.Task = Convert.ToString(preventivoView.SelectedRow.Cells[0].Text);//SelectedDataKey.Values[0]);
listaDaGridView.Add(budgetCommessa);
}
but i get a NullReferenceException even if the cell (0,0) of the GridView is populated
You should check if your gridview has any selected row. Apart from that, there are other problems in your code:
Rows.Count - 1.budgetCommesainside the for loop, otherwise you will end up with a list of object all pointing to the same value.So your code should be like:
If you are looking to get value from a cell from all rows, irrespective of selected then, you have to iterate each Row, instead of SelectedRow: