Wowywow
Member
- Joined
- Nov 9, 2019
- Messages
- 16
- Programming Experience
- Beginner
Hi, so I had to make this app that would take data from text file ( Name, Gender, bDay, etc...) and would load it into dataGridView table. Also to add to that there needs to be 3 buttons to load only (females, males or anyone older than *set age*.
I pretty much did what I needed, but each button has to read that file every time. So I'am wondering how can I have that file be read once and then leave the buttons just to act as filters that would set the data?
Thanks in advance!
I pretty much did what I needed, but each button has to read that file every time. So I'am wondering how can I have that file be read once and then leave the buttons just to act as filters that would set the data?
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Nuskaityti moterys
private void button1_Click(object sender, EventArgs e)
{
string systemPath = System.IO.Directory.GetCurrentDirectory() + @"\txt1.txt";
using (var streamReader = new System.IO.StreamReader(systemPath))
{
dataGridView1.Rows.Clear();
while (!streamReader.EndOfStream)
{
string line = streamReader.ReadLine();
string[] values = line.Split('\t');
for (int i=0; i<values.Length; i++)
{
string[] rez = values[i].Split(',');
rez[2] = rez[2].Trim();
if (rez[2].ToString() == "Moteris")
{
int rowIndex = dataGridView1.Rows.Add();
dataGridView1.Rows[rowIndex].Cells["Column1"].Value = rez[0];
dataGridView1.Rows[rowIndex].Cells["Column2"].Value = rez[1];
dataGridView1.Rows[rowIndex].Cells["Column3"].Value = rez[2];
dataGridView1.Rows[rowIndex].Cells["Column4"].Value = rez[3];
dataGridView1.Rows[rowIndex].Cells["Column5"].Value = rez[4];
dataGridView1.Rows[rowIndex].Cells["Column6"].Value = rez[5];
}
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string systemPath = System.IO.Directory.GetCurrentDirectory() + @"\txt1.txt";
using (var streamReader = new System.IO.StreamReader(systemPath))
{
dataGridView1.Rows.Clear();
while (!streamReader.EndOfStream)
{
string line = streamReader.ReadLine();
string[] values = line.Split('\t');
for (int i = 0; i < values.Length; i++)
{
string[] rez = values[i].Split(',');
rez[2] = rez[2].Trim();
if (rez[2].ToString() == "Vyras")
{
int rowIndex = dataGridView1.Rows.Add();
dataGridView1.Rows[rowIndex].Cells["Column1"].Value = rez[0];
dataGridView1.Rows[rowIndex].Cells["Column2"].Value = rez[1];
dataGridView1.Rows[rowIndex].Cells["Column3"].Value = rez[2];
dataGridView1.Rows[rowIndex].Cells["Column4"].Value = rez[3];
dataGridView1.Rows[rowIndex].Cells["Column5"].Value = rez[4];
dataGridView1.Rows[rowIndex].Cells["Column6"].Value = rez[5];
}
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
string systemPath = System.IO.Directory.GetCurrentDirectory() + @"\txt1.txt";
using (var streamReader = new System.IO.StreamReader(systemPath))
{
dataGridView1.Rows.Clear();
while (!streamReader.EndOfStream)
{
string line = streamReader.ReadLine();
string[] values = line.Split('\t');
for (int i = 0; i < values.Length; i++)
{
string[] rez = values[i].Split(',');
string date;
date = rez[5].Replace("-", "");
int today = int.Parse(DateTime.UtcNow.ToString("yyyyMMdd"));
int bDay = int.Parse(date);
int age = (today - bDay) / 10000;
int filterDate = Convert.ToInt32(textBox1.Text);
if (age >= filterDate)
{
int rowIndex = dataGridView1.Rows.Add();
dataGridView1.Rows[rowIndex].Cells["Column1"].Value = rez[0];
dataGridView1.Rows[rowIndex].Cells["Column2"].Value = rez[1];
dataGridView1.Rows[rowIndex].Cells["Column3"].Value = rez[2];
dataGridView1.Rows[rowIndex].Cells["Column4"].Value = rez[3];
dataGridView1.Rows[rowIndex].Cells["Column5"].Value = rez[4];
dataGridView1.Rows[rowIndex].Cells["Column6"].Value = rez[5];
}
}
}
}
}
}
}
Thanks in advance!