如何在没有硬编码值的C#Asp.net中的Grid View内动态加载CSV文件?
答案 0 :(得分:0)
if (File.Exists(myFileUpload.PostedFile.FileName))
{
string[] data = File.ReadAllLines(myFileUpload.PostedFile.FileName);
DataTable dt = new DataTable();
string[] col = data[0].Split(',');
foreach (string s in col)
{
dt.Columns.Add(s, typeof(string));
}
for (int i = 0; i < data.Length; i++)
{
string[] row = data[i].Split(',');
dt.Rows.Add(row);
}
myGridView.DataSource = dt;
myGridView.DataBind();
}