Reputation: 595
How to: Fill a DataGrid.DataContext with information from txt files (C#, WPF)
I am trying to fill my DataGrid
with information I get from some txt files. There is a folder called "CONTACTS" and there are some (5) files in. These files are configurated this way:
Content of John Doe.txt (without list symbols):
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#:
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:
DataGrid
with this Information.--> Solution by Dhaval Patel (see below).
Upvotes: 1
Views: 2439
Reputation: 7601
You can use the below mentioned code
Your Xaml Code looks like
<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
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
Upvotes: 1
Reputation: 2400
Maybe you should really read what is in your txt files.
By using for example a 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; }
}
Upvotes: 0
Reputation: 2615
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>
.
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.
Finally you bind the ItemsSource
of your DataGrid
to the PersonList
. That should do it.
Upvotes: 0