CSV Dosyasındaki Verileri dataGridView Nesnesinde Göstermek

 

Merhaba arkadaşlar bu makalemizde CSV dosyasındaki verileri datagridview nesnesinde göstereceğiz. Aşağıdaki şekildeki gibi bir CSV dosyamız olsun. İlk önce CSV dosyamızdaki ilk satırı header olarak tanımlıyoruz.

 


 






Şekil 1


 





Şekil 2

Form1.cs


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 datagridview_read_csv_file

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            string filePath = "D:\\customers.csv";

            DataTable dt = new DataTable();

            string[] rows = System.IO.File.ReadAllLines(filePath);

            if (rows.Length > 0)

            {

                // the first row is assigned as the column header in dataGridView

                // dgview de ilk satiri header atiyoruz.

                string firstRow = rows[0];

                string[] headers = firstRow.Split(',');

                foreach (string header in headers)

                {

                    dt.Columns.Add(new DataColumn(header));

                }

                // we get the data from the csv file

                // verileri csv den aliyoruz

                for (int i = 1; i < rows.Length; i++)

                {

                    string[] records = rows[i].Split(',');

                    DataRow dr = dt.NewRow();

                    int columnIndex = 0;

                    foreach (string data in headers)

                    {

                        dr[data] = records[columnIndex++];

                    }

 

                    dt.Rows.Add(dr);

                }

            }

            if (dt.Rows.Count > 0)

            {

                dataGridView1.DataSource = dt;

            }

 

        }

    }

 

}

 

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN

About Bahadır Şahin

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

2 comments:

gamezy dedi ki...

If simplicity matters, Gamezy offers a clean interface and easy navigation. The process to obtain a Cricket ID is straightforward, making it suitable for both new and experienced users. With transparent details about deposits and withdrawals, the platform helps users stay in control while enjoying sports and casino games.

Reddyanna dedi ki...

A reliable option for online cricket betting, Reddy Anna allows users to easily obtain a Cricket ID and start exploring features. The site shares helpful details about deposits and withdrawals, ensuring users feel confident while using it. With regular updates and a user-friendly interface, it creates a smooth experience.

Öne Çıkan Yayın

GridView da Seçili Satırı DetailsView da Göstermek

Merhaba arkadaşlar bu makalemizde GridView nesnesi ile birlikte DetailsView nesanesini birlikte kullanacağız. GridView da seçili satırın de...