csv转换为c#windows应用程序中的datagridview

本文关键字:datagridview 应用程序 c#windows 转换 csv | 更新日期: 2023-09-27 18:25:54

大家好,我有一个.csv文件,格式如下。我需要在c#windows应用程序的datagridview中以三列的形式显示这个值,如数字日期时间消息,其余的都将被忽略。。包括双引号、短信、快递请帮帮我我是新手请帮帮我。。

短信,交付,"+9919999999999",",",","2013.08.01 12:52","可出租3BHK Flat 102 1Floor@Mansarovar,mount happy rd.,Malabar Hill。West Seafacing。部分海景为斜坡上的建筑。Airy Well Lit Spacious1800地毯3BHK,商店+服务员rm+浴室宽度独立入口+百叶窗关闭的车库260capet,家具齐全的宽度白色商品可立即出租。"

短信,交付,"+9919999999999",",",","2013.08.01 09:40","在哈尔的以下公寓出租:SHIKHA,SHANJ附近,卡特路外,哈尔花园,面向1500平方英尺。3BHK半家具+1个停车位。租金为1.25拉克卢比。立即拥有。2小时通知检查。——Rohit J."

短信,发送,"+9919999999999",",","2013.07.31 20:37",","直接1bhk(Deepaksilverene@55Hill nsea@70Vindhyachal@702bhk Westside@80和谐@90Neptune@1.6大厦编号22@5530港元ElcoResidency@1.5Firdosmanzil@1.10Radhenarayan@1.25竖的bliss@2.754bhk Vinaykheights@4lacRawthanIgarden@1.75Cl/sms"

我试过这个代码。。

   private void button1_Click(object sender, EventArgs e)
    {
        if (System.IO.File.Exists("E:''messages.csv"))
        {
            System.IO.StreamReader fileReader = new StreamReader("E:''messages.csv");
            //Checking the end of file's content
            if (fileReader.Peek() != -1)
            {
                string fileRow = fileReader.ReadLine();
                string[] fileDataField = fileRow.Split(',');
                int count = fileDataField.Count();
                //Adding Column Header to DataGridView
                for (int i = 0; i < count; i++)
                {
                    DataGridViewTextBoxColumn columnDataGridTextBox = new DataGridViewTextBoxColumn();
                    columnDataGridTextBox.Name = fileDataField[i];
                    columnDataGridTextBox.HeaderText = fileDataField[i];
                    columnDataGridTextBox.Width = 120;
                    dataGridView1.Columns.Add(columnDataGridTextBox);
                }
                //Adding Data to DataGridView
                while (fileReader.Peek() != -1)
                {
                    fileRow = fileReader.ReadLine();
                    fileDataField = fileRow.Split(',');
                    dataGridView1.Rows.Add(fileDataField);
                }
            }
            //Close the StreamReader
            fileReader.Close();
        }
    }

csv转换为c#windows应用程序中的datagridview

你可以在linq中这样做

 var lines = File.ReadAllLines("yourcsvfile.csv").Select(a => a.Split(',')).ToList();
        foreach (string[] s in lines)
        {
            dataGridView1.Rows.Add(s[5], s[6], s[7]);
        }

顺便说一下,我试过你的输入,似乎有7列而不是3列。是否要排除任何?