问题
I'm iterating through the GridView rows and cells to get the values and store them in a 2D array.
This is my code:
for (int i = 0; i < GridView2.Rows.Count; i++)
{
for (int j = 0; j < (GridView2.Rows[i].Cells.Count - 3); j++)
{
// using the labels of the template fields.....
Label values = (Label)GridView2.Rows[i].Cells[j].FindControl("Label" + j);
GridValues[i,j] = values.Text; // gridvalues is my 2d array.
}
}
What I need now is to make the same values I got from the above loop to be added to a list(2D). So I created a class of that list
This is the code:
public class GridValuesClass
{
public string GridCustomerName { get; set; }
public string GridPickUpPoint { get; set; }
public DateTime GridPickUpDate { get; set; }
public TimeSpan GridDropPoint { get; set; }
public DateTime GridDropDate { get; set; }
public string GridCabStatus { get; set; }
public string GridAssignedCab { get; set; }
public TimeSpan GridActualDropTime { get; set; }
}
These properties represent the column names in the gridview.
So, how can I add the values in the gridview inside this class?
And how can I retrieve the values if my values are present in the list<GridValuesClass>
回答1:
You don't need to store it in a 2D array to be later stored in a list. Instead you can store it directly in to the list.
Sort of like this:
var list = new List<GridValuesClass>();
for (int i = 0; i < GridView2.Rows.Count; i++)
{
var job = new GridValuesClass {
GridPickUpPoint = GridView2.Rows[i].Cells[0]...,
GridPickUpDate = ...,
// and so on with possible use of conversions to get the correct types
}
list.Add(job);
}
Hope this helps.
As a side note, use more meaningful names.
- Give the class a more meaningful class name than
GridValuesClass, if you have several grid views then this name becomes useless. From what I gather on how you named the properties, it looks more like a job order for cabs. So maybeJobOrder? GridView2tells us nothing. What kind of data does the gridview contain? PerhapsJobsGridView?- Is it necessary to prefix all the fields with
Grid? What if the data is stored or used in a context where there is no grid view?
来源:https://stackoverflow.com/questions/13926636/how-to-add-the-gridview-cells-data-into-a-two-dimensional-list-and-retrieve-it