Create, Read, Update,Delete comma separated Value (csv) in c# winform without api

Опубликовано: 27 Октябрь 2024
на канале: IT Core Soft
925
18

Download from this link: https://github.com/hamza3344/crudoper...
My fiverr link: https://www.fiverr.com/hamzakhalid178
My Upwork Profile: https://www.upwork.com/freelancers/~0...

Whatsapp Number: +923338672398

contact me on [email protected] for custom software's.
Please share and like the video and subscribe the channel
If any question comment below
here is a code:

using System.IO;
using System.Linq;

string path = "file.csv";

private void Form1_Load(object sender, EventArgs e)
{
if(!File.Exists(path))
{
File.Create(path).Dispose();
using(TextWriter tw=new StreamWriter(path))
{
tw.WriteLine("id,name,location");
}
}
gridview();
}

public void gridview()
{
dataGridView1.DataSource = null;
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("Location");
string[] lines = File.ReadAllLines(path);
lines = lines.Skip(1).ToArray();
foreach(string line in lines)
{
DataRow row = dt.NewRow();
string[] values = line.Split(',');
int i = 0;
foreach(string value in values)
{
row[i] = value;
i = i + 1;
}
dt.Rows.Add(row);
}
dataGridView1.DataSource = dt;
}

private void add_Click(object sender, EventArgs e)
{
int count = 0;
if (File.Exists(path))
{
try
{
count = Convert.ToInt32(File.ReadAllLines(path).Last().Split(',')[0]);
count = count + 1;
}
catch(Exception e1)
{
count = 1;
}
File.AppendAllLines(path, new[] { count.ToString() + "," + txtname.Text + "," + txtlocation.Text });
}
gridview();
}
private void update_Click(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(path);
int j = 0;
foreach(string line in lines)
{
if(line.Split(',')[0]==lblid.Text)
{
break;
}
else
{
j = j + 1;
}
}
lines[j] = lines[j].Split(',')[0] + "," + txtname.Text + "," + txtlocation.Text;
File.WriteAllLines(path, lines);
gridview();
}

//delete code is not in description

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if(e.RowIndex>=0)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
lblid.Text = row.Cells[0].Value.ToString();
}
}