I am trying to import data from a csv to a gridview, but for some reason, I get the following error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Col5'.
Here is how the csv looks like:

I would like to display in the GridView in this same format, and set the column names and row names to "Q1, "Q2", "Q3", "Q4", "Q5".
P.S: I am a beginner at ASP.NET.
Here is my code:
aspx
aspx.cs
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string dir = @"Directory"; // Directory where the file exists string turnover_fi = dir + "\\turnover_ac_bull.csv"; FirstTurnoverGridViewRow(); DataTable dt = (DataTable)Session["currentTurnoverTable"]; //DataTable dt = new DataTable(); dt = (DataTable)ReadToEnd(turnover_fi); if (dt != null && dt.Rows.Count > 0) { TurnoverGridView.DataSource = dt; TurnoverGridView.DataBind(); } } } private object ReadToEnd(string filePath) { DataTable dtDataSource = new DataTable(); string[] fileContent = File.ReadAllLines(filePath); if (fileContent.Count() > 0) { string[] columns = fileContent[0].Split(','); for (int i = 0; i < columns.Count() - 1; i++) { dtDataSource.Columns.Add("Col" + (i + 1)); } for (int i = 0; i < fileContent.Count(); i++) { string[] row = fileContent.Split(',').Take(fileContent.Split(',').Length - 1).ToArray(); DataRow dr = dtDataSource.NewRow(); for (int j = 0; j < row.Length; j++) { dr[j] = row[j]; } dtDataSource.Rows.Add(dr); } } return dtDataSource; } protected void FirstTurnoverGridViewRow() { DataTable table = new DataTable(); string[] row_names = new string[] { "Q1", "Q2", "Q3", "Q4", "Q5" }; table.Columns.Add(new DataColumn("Col1", typeof(string))); table.Columns.Add(new DataColumn("Col2", typeof(double))); table.Columns.Add(new DataColumn("Col3", typeof(double))); table.Columns.Add(new DataColumn("Col4", typeof(double))); table.Columns.Add(new DataColumn("Col5", typeof(double))); table.Columns.Add(new DataColumn("Col6", typeof(double))); DataRow dr = table.NewRow(); for (int i = 0; i < row_names.Count(); i++) { dr = table.NewRow(); string text = row_names; dr["Col1"] = row_names; dr["Col2"] = DBNull.Value; dr["Col3"] = DBNull.Value; dr["Col4"] = DBNull.Value; dr["Col5"] = DBNull.Value; dr["Col6"] = DBNull.Value; table.Rows.Add(dr); } ViewState["currentTurnoverTable"] = table; TurnoverGridView.Visible = true; TurnoverGridView.DataSource = table; TurnoverGridView.DataBind(); }
Источник: https://stackoverflow.com/questions/241 ... -asp-net-c
Мобильная версия