Hacking Games
Hacking Games

Reputation: 63

Load .txt File to GridControl (DevExpress)

I was searching in the NET but didn't found anything useful for my case . I'am using DevExpress/GridControl ... and I have no idea how I can load .txt in GridControl with two Columns (Split on txt ==> '/t' )

Upvotes: 0

Views: 1444

Answers (1)

pixelbadger
pixelbadger

Reputation: 1596

DevExpress recommend using BindingList<T> to create binding data at runtime. You then assign this to the GridControl.DataSource property. See here for more info.

You would need to create an intermediate class (or struct) to hold a row of data from your text file:

public class TextFileData
{
    public TextFileData(string columnA, string columnB)
    {
        ColumnA = columnA;
        ColumnB = columnB;
    }

    public string ColumnA { get; set; }
    public string ColumnB { get; set; }
}

Then implement a method to open the file, sequentially read and convert the line to TextFileData and add it to your BindingList<TextFileData> instance. Something like:

public static class TextFileReader
{
    public static BindingList<TextFileData> Read(string path)
    {
        var list = new BindingList<TextFileData>();

        using (StreamReader sr = new StreamReader(path))
        {
            while (sr.Peek() >=0)
            {
                String line = sr.ReadLine();
                string[] columns = line.Split('\t')

                list.Add(new TextFileData(columns[0], columns[1]));
            }
        }

        return list;
    }
}

Upvotes: 1

Related Questions