Answer the question
In order to leave comments, you need to log in
How to load data into DataGridView from text file?
Hello. There is a DataGridView1 , the columns are already created and named. You need to load data from a text file into this table.
I wrote the following code to populate the table programmatically:
private void DataInput_Click(object sender, EventArgs e){
dataGridView1.Rows.Add("01.01.2022" "06:00" "Mersedes" "610-150" "6" "Иванов_Иван_Иванович" "Алексеев_Алексей_Алексеевич" 9999);
dataGridView1.Rows.Add("01.01.2022" "06:00" "Mersedes" "610-150" "6" "Иванов_Иван_Иванович" "Алексеев_Алексей_Алексеевич" 9999);
//И так далее
}
//Класс шаблон
using System;
using System.Collections.Generic;
using System.Text;
namespace Kursovaya
{
public class DataTemplate
{
public string DateWork { get; set; }
public string Time { get; set; }
public string BrandBus { get; set; }
public string CityNumber { get; set; }
public string RouteNumber { get; set; }
public string DriverName { get; set; }
public string CashierName { get; set; }
public int Income { get; set; }
public DataTemplate(string datework = "none",
string time = "none",
string brandbus = "none",
string citynumber = "none",
string routenumber = "none",
string drivername = "none",
string cashiername = "none",
int income = 0)
{
DateWork = datework;
Time = time;
BrandBus = brandbus;
CityNumber = citynumber;
RouteNumber = routenumber;
DriverName = drivername;
CashierName = cashiername;
Income = income;
}
}
}
//Класс для считывания данных из текстового файла
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kursovaya
{
public class LoadManager
{
private bool PathCheck(string check)
{
if (!Directory.Exists(@"data"))
{
Directory.CreateDirectory(@"data");
return false;
}
else
{
if (File.Exists(check))
{
return File.ReadAllText(check).Length != 0;
}
return false;
}
}
public Dictionary<string, DataTemplate> InputData()
{
Dictionary<string, DataTemplate> inputdata = new Dictionary<string, DataTemplate>();
//Загрузка данных, если существуют
if (PathCheck(@"data\inputdata.txt"))
{
// Чтение файла сохранения и создания из него массива
string[] tmpRows = File.ReadAllText(@"data\inputdata.txt").Trim().Split('\n');
string[] tmpColumns;
foreach (string item in tmpRows)
{
tmpColumns = item.Split('>');
// Создаём новые данные
DataTemplate datatemplate = new DataTemplate(tmpColumns[0],
tmpColumns[1],
tmpColumns[2],
tmpColumns[3],
tmpColumns[4],
tmpColumns[5],
tmpColumns[6],
int.Parse(tmpColumns[7])
);
// Добавляем данные в словарь
inputdata.Add(datatemplate.DateWork, datatemplate);
}
}
return inputdata;
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kursovaya
{
public class SaveManager
{
private void PathCheck()
{
if (!Directory.Exists(@"data"))
{
Directory.CreateDirectory(@"data");
}
}
public void InputData(Dictionary<string, DataTemplate> inputdata)
{
PathCheck();
StringBuilder stringBuilder = new StringBuilder();
foreach (var item in inputdata)
{
stringBuilder.Append(string.Join(">", new string[]
{
item.Value.DateWork,
item.Value.Time,
item.Value.BrandBus,
item.Value.CityNumber,
item.Value.RouteNumber,
item.Value.DriverName,
item.Value.CashierName,
item.Value.Income.ToString()
}));
stringBuilder.Append("\n");
}
// Запись в файл
File.WriteAllText(@"data\inputdata.txt", stringBuilder.ToString());
}
}
}
private void button1_Click(object sender, EventArgs e)
{
SaveManager saveManager = new SaveManager();
saveManager.InputData(datatemplate);
}
Answer the question
In order to leave comments, you need to log in
.txt as a datasource for datagridview
How To Import Text File Data Into Datagridview In C#
You can just read the file line by line and enter it with this code
dataGridView1.Rows.Add("01.01.2022" "06:00" "Mersedes" "610-150" "6" "Иванов_Иван_Иванович" "Алексеев_Алексей_Алексеевич" 9999);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question