文本文件作为数据源,以在网格视图中显示数据

本文关键字:视图 网格 显示 数据 文件 数据源 文本 | 更新日期: 2023-09-27 18:30:57

文本文件数据如下所示:

设施

ID:12787 设施名称:ACME医疗中心 设施位置:XYZ

设施

ID:27875 设施名称:医疗中心 设施位置:kh

private void ReadFile(string fileName)
        {               
            var rows = System.IO.File.ReadAllLines(fileName);
            Char[] separator = new Char[] { ':' };
            DataTable tbl = new DataTable(fileName);
            if (rows.Length != 0)
            {
                foreach (string headerCol in rows[0].Split(separator[0]))
                {
                    tbl.Columns.Add(new DataColumn(headerCol));
                }
                if (rows.Length > 1)
                {
                    for (int rowIndex = 1; rowIndex < rows.Length; rowIndex++)
                    {
                        var newRow = tbl.NewRow();
                        var cols = rows[rowIndex].Split(separator);
                        for (int colIndex = 0; colIndex < cols.Length; colIndex++)
                        {
                            newRow[colIndex] = cols[colIndex];
                        }
                        tbl.Rows.Add(newRow);
                    }
                }
            }
        }

在上面编写的代码中的数据表中添加数据。但它没有正确填充。"数据表填写错误"

FacilityID:12787 
FacilityName:ACME Medical Center 
FacilityLocation:XYZ
FacilityID:27875 
FacilityName:Medical Center 
FacilityLocation:kh

我应该如何修改数据表应该填充的代码,如下所示

 FacilityID  FacilityName        FacilityLocation
    12787    ACME Medical Center XYZ
    27875    Medical Center      kh 

文本文件作为数据源,以在网格视图中显示数据

网上已有的一些东西可能对你有帮助请浏览以下链接:

http://www.codeproject.com/KB/database/ReadTextFile.aspxhttp://aspalliance.com/1107_CodeSnip_Reading_CSV_Files_Using_Dataset.allhttp://www.c-sharpcorner.com/UploadFile/mgold/ConnectODBCText11262005070206AM/ConnectODBCText.aspx

   private string GetID(string str)
    {
        if (!string.IsNullOrEmpty(str.Split(':')[1]))
        {
            return str.Split(':')[1].Replace("FacilityName", string.Empty);
        }
        return string.Empty;
    }
    private string GetName(string str)
    {
        if (!string.IsNullOrEmpty(str.Split(':')[2]))
        {
            return str.Split(':')[2].Replace("FacilityLocation", string.Empty);
        }
        return string.Empty;
    }
    private string GetLocation(string str)
    {
        if (!string.IsNullOrEmpty(str.Split(':')[3]))
        {
            return str.Split(':')[3];
        }
        return string.Empty;
    }

    private string Button Click()
    {
        string rows = "FacilityID:12787 FacilityName:ACME Medical Center FacilityLocation:XYZ";
        DataTable tbl = new DataTable("test");
        if (rows.Length != 0)
        {
            tbl.Columns.Add("FacilityID");
            tbl.Columns.Add("FacilityName");
            tbl.Columns.Add("FacilityLocation");
            var newRow = tbl.NewRow();
            newRow[0] = GetID(rows);
            newRow[1] = GetName(rows);
            newRow[2] = GetLocation(rows);
            tbl.Rows.Add(newRow);
            dataGridView1.DataSource = tbl.DefaultView;
        }
    }