0

How can I bind a text file to a datagrid with C# WPF? The idea is to have a row in the text file to show as a row in datagrid.

0

2 Answers 2

4

I don't think you can bind text directly to a datagrid

What you can do however is bind an objet to a datagrid

create an objet representing your text file.

-- content --  
text1, param1, param2  
text2, param1, param2  

class OneLine{
  string text {get;set;}  
  string param { get;set; }
  ...
}

You can then bind those objects to the datagrid with a BindingList, which is mostly a List. The magic lies in the Properties of the object. The BindingList will try to get each property of the object and display them in the grid.

BindingList<OneLine> myList = new BindingList<OneLine>();
myList.Add(oneObject);

DataGrid myGrid = new DataGrid();
myGrid.DataSource = myList;
Sign up to request clarification or add additional context in comments.

Comments

1

In my project I use the following approach

  1. Create class that represent row in text file for example

    public class cls_syslog_record
    {
        public DateTime? f1 {get;set;}
        public string f2 {get;set;}                
        public string f3 {get;set;}
        public string f4 {get;set;}        
    }
    
  2. Create IEnumerable that used as source for DataGrid

    public IEnumerable<cls_syslog_record> get_line_seq_text()
    {
        cls_mvs_syslog_parser parser = new cls_mvs_syslog_parser();
        foreach (string record_line in File.ReadLines(this.filename))
        {
            cls_syslog_record text_record = parser.parse_syslog_text(record_line);
            if (text_record == null)
            {
                 continue;
            }
            yield return text_record;
         }
    }
    
  3. set my IEnumerable object as source

    static private DataGrid make_text_viewer(string p_filename)
    {
        logger.Debug("start");
        DataGrid table_viewer;
        cls_file_line_seq fl_seq = new cls_file_line_seq(p_filename);
        table_viewer = new DataGrid();
        table_viewer.CanUserAddRows = false;
        table_viewer.CanUserDeleteRows = false;
        table_viewer.Columns.Add(create_column("Date Time", "timestamp"));
        table_viewer.Columns.Add(create_column("LPAR Name", "lpar_name"));
        table_viewer.Columns.Add(create_column("JOB ID", "job_id"));
        table_viewer.Columns.Add(create_column("Message", "message"));
        table_viewer.HeadersVisibility = DataGridHeadersVisibility.All;
        table_viewer.ItemsSource = fl_seq.get_line_seq_text();
        return table_viewer;
    }
    
  4. Then setup binding

    static private DataGridColumn create_column(string header, string p_property_name)
    {
        DataGridTextColumn column = new DataGridTextColumn();
        column.Header = header;
        column.Binding = new Binding(p_property_name);            
        return column;
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.