terrazo
terrazo

Reputation: 85

Displaying data in datagridview C#

I have a problem I would like to display the data in a DataGridView. The script gets the deal with all the txt files and searches using regular expression data in each file. Everything works nicely.

My problem is that I just do not know how to display the results now in DataGridView;

(As if anyone could help me how I do it. Thank you in advance for your help.

private void button2_Click(object sender, EventArgs e)
{
    string newPath = (Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\faktury\\");
    string[] filePaths = Directory.GetFiles(newPath, "*.txt");

    foreach (string fp in filePaths)
    {
        string[] lines = File.ReadAllLines(fp);

        // Iterate through lines
        foreach (string line in lines)
        {

            foreach (Match match in Regex.Matches(line, @"(Numer Faktury:|Numer faktury korygującej: )(.*?)$", RegexOptions.IgnoreCase))
            {

                MessageBox.Show(match.Groups[2].Value);

            }
            foreach (Match match in Regex.Matches(line, @"Data wystawienia: (.*?)$", RegexOptions.IgnoreCase))
            {

                MessageBox.Show(match.Groups[1].Value);

            }
            foreach (Match match in Regex.Matches(line, @"Wartość netto  (.*?) PLN", RegexOptions.IgnoreCase))
            {

                MessageBox.Show(match.Groups[1].Value);

            }
            foreach (Match match in Regex.Matches(line, @"Wartość całkowita VAT 8 %  (.*?) PLN", RegexOptions.IgnoreCase))
            {

                MessageBox.Show(match.Groups[1].Value);

            }
            foreach (Match match in Regex.Matches(line, @"Wartość brutto  (.*?) PLN", RegexOptions.IgnoreCase))
            {

                MessageBox.Show(match.Groups[1].Value);

            }
        }

Upvotes: 1

Views: 111

Answers (1)

Mong Zhu
Mong Zhu

Reputation: 23732

You could collect all your results in a List<T> and bind it in the end as DataSource to the DataGridView

For that I would suggest to make a extra class with properties to be displayed:

public class ShowResults
{
    public string MatchValue { get; set; }

    // you can of course add as much properties as you want to be display
    // depending on what information you want to share with the user

    public ShowResults(string mv)
    {
        this.MatchValue = mv;
    }    
}

Then go ahead and collect your results in a List<ShowResults>

string newPath = (Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\faktury\\");

string[] filePaths = Directory.GetFiles(newPath, "*.txt");

List<ShowResults> results = new List<ShowResults>();

foreach (string fp in filePaths)
{
    string[] lines = File.ReadAllLines(fp);

    foreach (Match match in Regex.Matches(line, @"(Numer Faktury:|Numer faktury korygującej: )(.*?)$", RegexOptions.IgnoreCase))
    {
        results.Add(new ShowResults(match.Groups[2].Value));
    }
    foreach (Match match in Regex.Matches(line, @"Data wystawienia: (.*?)$", RegexOptions.IgnoreCase))
    {
        results.Add(new ShowResults(match.Groups[1].Value));
    }

//... and so on 

in the end you would bind it.

dataGridView1.DataSource = results;

Upvotes: 1

Related Questions