Populate datagrid using Datacontext in C# WPF

Posted by anon on Stack Overflow See other posts from Stack Overflow or by anon
Published on 2010-05-18T18:34:41Z Indexed on 2010/05/18 18:50 UTC
Read the original article Hit count: 491

Filed under:
|
|
|

I'm trying to get data from file. The file first has three lines of text that describes the file, then there is a header for the data below. I've managed to extract that. What I'm having problems is getting the data below the header line. The data below the header line can look like "1 2 3 4 5 6 7 8 9 abc".

DataGrid dataGrid1 = new DataGrid();
masterGrid.Children.Add(dataGrid1);

using (TextReader tr = new StreamReader(@filename))
{
    int lineCount = 1;
    while (tr.Peek() >= 0)
    {
        string line = tr.ReadLine();
        {
            string[] data = line.Trim().Split(' ');

            Dictionary<string, object> sensorData = new Dictionary<string, object>();

            for (int i = 0, j = 0; i < data.Length; i++)
            {
                //I know that I'm delimiting the data by a space before.
                //but the data from the text file doesn't necessarily have
                //just one space between each piece of data
                //so if I don't do this, spaces will become part of the data.
                if (String.Compare(data[i]," ") > 0)
                {
                    sensorData[head[j]] = data[i];
                    j++;
                }
            }

            sensorDatas.Add(sensorData);

            sensorData = null;

        }
        lineCount++;
    }
}

dataGrid1.DataContext = sensorDatas;

I can't figure out why this doens't work. If I change "dataGrid1.DataContext = sensorDatas;" to "dataGrid1.ItemsSource = sensorDatas;" then I get the data in the proper columns, but I also get some Raw View data such as: Comparer, Count, Keys, Values as columns, which I don't want.

Any insight?

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf