4 Replies - 4129 Views - Last Post: 13 May 2010 - 08:00 AM Rate Topic: -----

#1 heckler01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-May 10

Importing ASCII file into WPF Datagrid using C#

Posted 12 May 2010 - 11:07 AM

Hi, I just started programming in C# and using WPF so pardon my ignorance.
I'm creating an WPF application where I need to dynamically make a grid. The grid headers will be different every time based on information in the text file and I will only need this grid if the user opens it.

So right now, I'm able to brows for a file and get the path. Then after I create a datagrid, like this:

//Create a new data grid
DataGrid datagrid1 = new DataGrid();
Master.Children.Add(datagrid1);
Grid.SetRow(datagrid1, 1);
Grid.SetColumn(datagrid1, 1);



Now, I have issues accessing the file and populating the grid. How would I be able to do this in C#?

Is This A Good Question/Topic? 0
  • +

Replies To: Importing ASCII file into WPF Datagrid using C#

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Importing ASCII file into WPF Datagrid using C#

Posted 12 May 2010 - 11:19 AM

moved to the WPF forum.

can you give an example of what the contents of the file will be, and how that data maps to the Grid?
Was This Post Helpful? 0
  • +
  • -

#3 heckler01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-May 10

Re: Importing ASCII file into WPF Datagrid using C#

Posted 12 May 2010 - 11:27 AM

View Posteclipsed4utoo, on 12 May 2010 - 10:19 AM, said:

moved to the WPF forum.

can you give an example of what the contents of the file will be, and how that data maps to the Grid?



The file will first have this header:
Time x y speed_x speed_y acc_x acc_y Target Leg Type
The header can have more paramaters depending on the file.

then it will have an unknown amount of row of data like this:
0.00 47.50 -42.50 -1.00 0.00 0.00 0.00 1 1 Sensor_1

This post has been edited by heckler01: 12 May 2010 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Importing ASCII file into WPF Datagrid using C#

Posted 12 May 2010 - 01:56 PM

This was actually quite fun...

here is the datafile that I used...
Time x y speed_x speed_y acc_x acc_y Target Leg Type
0.00 47.50 -42.50 -1.00 0.00 0.00 0.00 1 1 Sensor_1
0.00 47.50 -42.50 -1.00 0.00 0.00 0.00 1 1 Sensor_2
0.00 47.50 -42.50 -1.00 0.00 0.00 0.00 1 1 Sensor_3
0.00 47.50 -42.50 -1.00 0.00 0.00 0.00 1 1 Sensor_4
0.00 47.50 -42.50 -1.00 0.00 0.00 0.00 1 1 Sensor_5



here is the code...
ObservableCollection<Dictionary<string, object>> sensorDatas = new ObservableCollection<Dictionary<string, object>>();
List<string> keys = new List<string>();

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    using (TextReader tr = new StreamReader(@"C:\test\test.txt"))
    {
        int lineCount = 1;
        while (tr.Peek() >= 0)
        {
            string line = tr.ReadLine();
                    
            if (lineCount == 1)
            {
                string[] headers = line.Trim().Split(' ');

                foreach (string h in headers)
                {
                    CreateGridColumn(h);
                    keys.Add(h);
                }
            }
            else
            {
                string[] data = line.Trim().Split(' ');
               
                // creates a dictionary where the column name is the key and
                //    and the data is the value
                Dictionary<string, object> sensorData = new Dictionary<string, object>();

                for (int i = 0; i < data.Length; i++)
                {
                    sensorData[keys[i]] = data[i];
                }

                sensorDatas.Add(sensorData);

                sensorData = null;
            }

            lineCount++;
        }
    }
            
    dataGrid1.DataContext = sensorDatas;
}

private void CreateGridColumn(string headerText)
{
    dataGrid1.Columns.Add(new DataGridTextColumn
    {
        Header = headerText,
        Binding = new Binding(string.Format("[{0}]", headerText))
    });
}



and here is the XAML for the Grid..
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="692">
    <Grid Width="673">
        <DataGrid 
            AutoGenerateColumns="False" 
            Height="287" 
            HorizontalAlignment="Left" 
            Margin="12,12,0,0" 
            Name="dataGrid1" 
            VerticalAlignment="Top" 
            Width="649"
            ItemsSource="{Binding}">
        </DataGrid>
    </Grid>
</Window>


This post has been edited by eclipsed4utoo: 12 May 2010 - 02:01 PM

Was This Post Helpful? 2
  • +
  • -

#5 heckler01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-May 10

Re: Importing ASCII file into WPF Datagrid using C#

Posted 13 May 2010 - 08:00 AM

View Posteclipsed4utoo, on 12 May 2010 - 12:56 PM, said:

This was actually quite fun...


Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1