0

Hi,

my task is to read a text file and display it in a DataGridView and to be able to filter the data. Each column in a DataGridView will be a string from a file, separated by "," -> column1, column2, column3... Each line will be a new row. The number of columns can differ, but always first column is a DateTime and the other are strings. I want to solve it by MVC. I know two ways how to do this, but I'am looking for a best solution. So far I've implemented(it's ugly, simplified code, not the real one):

My model would be:

public class Item
{
    public DateTime SampleDate;  
    public List<String> Columns = new List<String>(); //this stores all columns
}

public class ItemList : List<Item>
{

    public void GetDataFromFile()
    {
        //hier I read data line by line from a file, and split each line to get column
        foreach(var row in "rows read from a file")
        {
            Item myRow = new Item();
            myRow.SampleDate = "data column read from a file";
            foreach(var col in "columns read from a file")
            {
                myRow.Columns.Add("col");
            }
            this.Add(myRow)
        }

    }

    public void FilterData(string criteria)
    {
        var query = LINQ??? //how to implement filtering, can I use DataView in a Model? 
    }
}

So far everything is fine until I try to filter the data. When I get the data from the model to the view, there I place it in a DataTable, combine it with DataView, I can use filtering on DatView according to user input(this woluld be a one way to solve it). But I think, the proper way in MVC would be to get the user input in the View, propagate it to the Model, generate filtered data, and give them back to the view. Am I right? If so, how should I filter the list, let say, I want all rows, where column2 contains "abc" string. Am I allowed to use DataView in the Model or would this break the MVC rule?
Because of the fact, that the column count can differ, I want to use a List<String> in the Item class. I don't want to have properties for each column - public string column1Prop{}; string column2Prop{}..., because I don't know the count of columns. If I had the properties, this would be easy to for filtering usind linq. But how can I filter it when I only have List<string>? I'am using .net 3.5, winforms.
If anybody knows better way to solve the problem, please let me know. Thanks.

Martin

1
Contributor
1
Reply
9
Views
2 Years
Discussion Span
Last Post by martin3885
0

As addtion to my post from yesterday, I've managed to find a way to filter the data using linq:

public void FilterData(string criteria)
{
    var query = this.Where(x => ((Item)x).Columns[3] == criteria).ToList();
}

However the question about usind DataView in MVC - "Am I allowed to use DataView in the Model or would this break the MVC rule?" is still open. I would appreciate your mind.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.