这可能是不允许的,但我还是会问。
我正在尝试开发一种类似于 YALV 但用于自定义日志的产品。
我有一个 CSV 文件,我想读入该文件并在屏幕上显示在一个表格中,该表格可以滚动、过滤、着色等,类似于 YALV。
我有用于读取和破译 CSV 并以简洁的格式输出到另一个文件的代码。我想对其进行调整以将其搬上屏幕。
我有 C# 和 C 经验,但仅与控制台相关。有人可以指导我正确的方向,至少让文件以与 YALV 类似的格式在窗口中读取吗?我可能采取的步骤是什么?我不知道从哪里开始......
Thanks
下面的代码在 DataGridView 中显示结果
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const string FILENAME = @"c:\temp\test.csv";
private void button1_Click(object sender, EventArgs e)
{
CSVReader csvReader = new CSVReader();
DataSet ds = csvReader.ReadCSVFile(FILENAME, true);
dataGridView1.DataSource = ds.Tables["Table1"];
}
}
public class CSVReader
{
public DataSet ReadCSVFile(string fullPath, bool headerRow)
{
string path = fullPath.Substring(0, fullPath.LastIndexOf("\\") + 1);
string filename = fullPath.Substring(fullPath.LastIndexOf("\\") + 1);
DataSet ds = new DataSet();
try
{
if (File.Exists(fullPath))
{
string ConStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}" + ";Extended Properties=\"Text;HDR={1};FMT=Delimited\\\"", path, headerRow ? "Yes" : "No");
string SQL = string.Format("SELECT * FROM {0}", filename);
OleDbDataAdapter adapter = new OleDbDataAdapter(SQL, ConStr);
adapter.Fill(ds, "TextFile");
ds.Tables[0].TableName = "Table1";
}
foreach (DataColumn col in ds.Tables["Table1"].Columns)
{
col.ColumnName = col.ColumnName.Replace(" ", "_");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return ds;
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)