views:

37

answers:

1

Hello,
I understand that i cannot do something like List<List<string,string>>. So i have to put each object in a class, and then have a list of classes. Below is the problem as I understand it.

  • Class FailedField has properties FieldName, Error
    • sample data: "year", "must be between 1900-2100"
  • Class FailedFields is a list of FailedField; Meaning a list of FailedField per record
    • sample data: { ("year", "must be between 1900-2100" ),("percentage", "must be between 0-100").... }
  • Class FailedFieldsList is a list of FailedFields for multiple records

In case you are wondering why I am trying to implement this data structure, i am collecting validation errors for each failed attempt to insert a record in LINQ-to-SQL.

My code so far:

In the partial class representing one SQL table:

public partial class FailedFields 
{
    public List<FailedField> ListOfFailedFieldsInOneRecord = new List<FailedField>();
    public void addFailedField(FailedField f) 
    {
        ListOfFailedFieldsInOneRecord.Add(f);
    }
    public int getFailedFieldsCount() 
    {
        return ListOfFailedFieldsInOneRecord.Count();
    }
}
public partial class FailedField
{
    public string fieldName;
    public string message;
    public FailedField(string vField, string vMessage)
    {
        this.fieldName = vField;
        this.message = vMessage;
    }
    public override string ToString()
    {
        return fieldName + ", " + message;
    }
}

In the MyDataContext

public partial class MyClassesDataContext
{
  public partial class FailedFieldsList
  {
    private static List<Slab.FailedFields> ErrorList = new List<Slab.FailedFields>();
    public void AddErrorList(Slab.FailedFields errs)
    {
        ErrorList.Add(errs);
    }
    public List<Slab.FailedFields> GetErrorList()
    {
        return ErrorList;
    }
  }
}

And in Codebehind

MyClassesDataContext.FailedFieldsList FailedRecords = new  MyClassesDataContext.FailedFieldsList();
ErrorBoxGridView.DataSource = FailedRecords.GetErrorList();
ErrorBoxGridView.DataBind();

Sample output

|------------------------------------------------------------------------------|
| Record # | Field_1 | Error_1 | Field_2 | Error_2 | ... | Field_10 | Error_10 |
|------------------------------------------------------------------------------|
|   Data   |   Data  |   Data  |   Data  |   Data  | ... |   Data   |   Data   | 
|------------------------------------------------------------------------------|

From Visual Studio debugger, I know I am filling the objects upto the FailedFieldsList class. I am not sure where i am going wrong. Either the logic of the data structure is wrong, or i am not accessing the object correctly. Or maybe List is not the best tool in this case?

If there is something you think would help me understand where i am going wrong, please suggest any reference material you have.

I am stuck. Any help appreciated.

+1  A: 

It sounds like you're trying to bind a list of list of FailedField records to a grid.

The problem is that the Grid will display what's in your (first) list, in this case each record in your list is itself a list, so what do you actually want the grid to show?

You either need to flatten your list of lists down to one list (with something to identify the groups if you like), or you could use a master/detail type of view if there's info about each list of string pairs you want to display, or you could use a tree to allow drilling down to the level you want.

Think about what you actually want to display and work backwards from there.

Timothy Walters