将 .txt 文件加载到 gridview c# [英] load .txt file into gridview c#

查看:66
本文介绍了将 .txt 文件加载到 gridview c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我快到了,只需要从记事本中的文本文件中加载信息,保存方式如下:

So im nearly there, just need to load the information from my text file in notepad which is saved like so:

项目 1

项目 2

item3

item4

我的第一次尝试是这样的:

My first attempt was this:

gridProfiles.Columns.Add("App Name", "Active");
        int i = 0;
        foreach(char snif in "D:\\ProfileAppName.txt")
        {
            gridProfiles.Rows.Add();
            gridProfiles.Rows[i].Cells[0].Value = snif;
            i++;
        }

但这只是将文件名加载到文本文件中

But this just loaded the file name into the text file

我的下一次尝试是:

 gridProfiles.Columns.Add("App Name", "Active");
        int i = 0;
        foreach (string snif in "D:\\ProfileAppName.txt")
        {
            string[] values = snif.Split(' ');
            int j = 0;
            foreach (string value in values)
            {
                gridProfiles.Rows.Add();
                gridProfiles.Rows[i].Cells[0].Value = snif;
                j++;
            }
        }
        i++;

但我收到错误无法将类型'char'转换为'string'

But i get the error "cannot convert type 'char' into 'string'

谁能帮帮我?我快到了,但我无法抓住它

Can anyone help me? im nearly there but cant get my finger on it

推荐答案

读取文件内容你可以这样做:

The read the content of the file you could do this:

int i = 0;
foreach(string row in File.ReadAllLines("D:\\ProfileAppName.txt"))
{
    if (i % 2 == 0)
        gridProfiles.Rows.Add();
    gridProfiles.Rows[i / 2].Cells[i % 2].Value = line;
    i++;
}

这篇关于将 .txt 文件加载到 gridview c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆