简体   繁体   English

如何:使用txt文件(C#,WPF)中的信息填充DataGrid.DataContext

[英]How to: Fill a DataGrid.DataContext with information from txt files (C#, WPF)

How to: Fill a DataGrid.DataContext with information from txt files (C#, WPF) 如何:使用txt文件(C#,WPF)中的信息填充DataGrid.DataContext

I am trying to fill my DataGrid with information I get from some txt files. 我正在尝试使用从一些txt文件中获取的信息来填充DataGrid There is a folder called "CONTACTS" and there are some (5) files in. These files are configurated this way: 有一个名为“ CONTACTS”的文件夹,并且其中包含一些(5)文件。这些文件的配置方式如下:

Content of John Doe.txt (without list symbols): John Doe.txt的内容(不带列表符号):

  • John Doe Corp. 约翰·多伊公司
  • Mr. 先生。
  • Doe 母鹿
  • John 约翰

XAML: XAML:

<DataGrid AutoGenerateColumns="False" CanUserResizeRows="False" DockPanel.Dock="Bottom" ItemsSource="{Binding CollectionofDetails}" Name="dataGrid_Content">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding CorporationName}" Header="Firma" />
                    <DataGridTextColumn Binding="{Binding Prefix}" Header="Anrede" />
                    <DataGridTextColumn Binding="{Binding FirstName}" Header="Nachname" />
                    <DataGridTextColumn Binding="{Binding LastName}" Header="Vorname" />
                </DataGrid.Columns>
            </DataGrid>

C#: C#:

public class Details
    {
        public string CorporationName { get; set; }
        public string Prefix { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public ObservableCollection<Details> _CollectionofDetails = new ObservableCollection<Details>();

    public ObservableCollection<Details> CollectionofDetails
    {
        get { return _CollectionofDetails; }
        set { _CollectionofDetails = value; }
    }

    public void SetItemsToDataContext()
    {
        foreach (string Datei in Directory.GetFiles(@"C:\Users\schwarz\Desktop\Cusposes_2014-05-20_0827\ISAPO\ISAPO Cusposes\Business Contacts\Contacts", "*.txt"))
        {
            StreamReader reader = new StreamReader(Datei);

            int i = 0;
            string line = reader.ReadToEnd().Replace("\n", "");
            string[] t = line.Split('\r');
            Details d = new Details();
            d.CorporationName = t[i];
            d.Prefix = t[i + 1];
            d.FirstName = t[i + 2];
            d.LastName = t[i + 3];

            CollectionofDetails.Add(d);
            reader.Close();
        }
    }

Unfortunately, I do have the following problems: 不幸的是,我确实存在以下问题:

  1. I do not know how to load THE CONTENT of each file (for all files). 我不知道如何加载每个文件的内容(对于所有文件)。
  2. I do not know how to fill my DataGrid with this Information. 我不知道如何用此信息填充我的DataGrid

--> Solution by Dhaval Patel (see below). -> Dhaval Patel解决方案(请参阅下文)。

You can use the below mentioned code 您可以使用下面提到的代码

Your Xaml Code looks like 您的Xaml代码看起来像

 <DataGrid AutoGenerateColumns="False" CanUserResizeRows="False" DockPanel.Dock="Bottom" ItemsSource="{Binding CollectionofDetails}" Name="dataGrid_Content">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding CorporationName}" Header="Firma" />
            <DataGridTextColumn Binding="{Binding Prefix}" Header="Anrede" />
            <DataGridTextColumn Binding="{Binding FirstName}" Header="Nachname" />
            <DataGridTextColumn Binding="{Binding LastName}" Header="Vorname" />
        </DataGrid.Columns>
    </DataGrid>


 <Button Command="{Binding MyCommand}" Width="100" Margin="346,230,346,-189">RunMyCommand</Button>

Your ViewModel code look like 您的ViewModel代码看起来像

 private ObservableCollection<Details> _CollectionofDetails=new ObservableCollection<Details>();

    public ObservableCollection<Details> CollectionofDetails
    {
        get { return _CollectionofDetails; }
        set { _CollectionofDetails = value; RaisePropertyChanged("CollectionofDetails"); }
    }
 private RelayCommand _MyCommand;

    public RelayCommand MyCommand
    {
        get { return _MyCommand??(_MyCommand=new RelayCommand(Methodcall)); }
        set { _MyCommand = value; }
    }
  void Methodcall()
    {

        foreach (string Datei in Directory.GetFiles(@"C:\textfile", "*.txt"))
        {
            StreamReader reader = new StreamReader(Datei);


            int i=0;
            string line = reader.ReadToEnd().Replace("\n","");
            string[] t = line.Split('\r');
            Details d = new Details();
            d.CorporationName = t[i];
            d.Prefix = t[i + 1];
            d.FirstName = t[i + 2];
            d.LastName = t[i + 3];

            CollectionofDetails.Add(d);
            reader.Close();



        }
    }

public class Details
{
    public string CorporationName { get; set; }
    public string Prefix { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

output should look like 输出应该看起来像

在此处输入图片说明

This is a problem that you have to break into pieces. 这是一个必须分解的问题。 I would recommend that you define a class suitable for your data, like 我建议您定义一个适合您数据的类,例如

public class Person {

    public string FullName {get; set;}
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public string Title {get; set;}

}

and in your viewmodel class you have a variable PersonList of type List<Person> . 并且在您的viewmodel类中,您具有类型为List<Person> PersonList变量。

Then your have to write a method, something like 然后,您必须编写一个方法,例如

private void PopulatePersonData(){

    // Here you put all the logic reading the .txt files creating a person for each file
    // and adding the person to the PersonList.

}

which you then call in the constructor of the viewmodel. 然后在viewmodel的构造函数中调用它。

Finally you bind the ItemsSource of your DataGrid to the PersonList . 最后,将DataGridItemsSource绑定到PersonList That should do it. 那应该做。

Maybe you should really read what is in your txt files. 也许您应该真正阅读txt文件中的内容。
By using for example a Reader : 通过使用例如Reader:

private void SetItemsToDataContext()
{
    foreach (String Datei in Directory.GetFiles(@"C:\Contacts", "*.txt"))
    {
        String[] linesRead = File.ReadAllLines(Datei);
        if (linesRead.Length != 4)
        {
            continue;
        }
        Contact contactRead = new Contact();
        contactRead.Company = linesRead[0];
        contactRead.Gender = linesRead[1];
        contactRead.Name = linesRead[2];
        contactRead.FirstName = linesRead[3];
        dataGrid_Content.Items.Add(contactRead);
    }
}

public class Contact
{
    public String Company { get; set; }
    public String Gender { get; set; }
    public String Name { get; set; }
    public String FirstName { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM