简体   繁体   中英

Importing data from csv to GridView ASP.NET/C#

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".

Could anyone please help? Thank you in advance. PS: I am a beginner at ASP.NET

Here is my code:

aspx

<asp:GridView ID="TurnoverGridView" runat="server" ShowHeaderWhenEmpty="True" AutoGenerateColumns="False"
                Width="318px" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True">
                <AlternatingRowStyle BackColor="White" Height="2px" />
                <Columns>
                    <asp:TemplateField HeaderText="">
                        <ItemStyle Font-Size="13px" Width="10%" />
                        <ItemTemplate>
                            <asp:Label ID="RowLabel" runat="server" ReadOnly="true" Text='<%# Eval("Col1") %>'
                                Style="text-align: left; font-weight:bold"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Q1">
                        <ItemStyle Font-Size="13px" Width="16%" />
                        <ItemTemplate>
                            <asp:Label ID="FirstLabel" runat="server" ReadOnly="true" Text='<%# Eval("Col2") %>'
                                Style="text-align: left;"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Q2">
                        <ItemStyle Font-Size="13px" Width="16%" />
                        <ItemTemplate>
                            <asp:Label ID="SecondLabel" runat="server" ReadOnly="true" Text='<%# Eval("Col3") %>'
                                Style="text-align: left;"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Q3">
                        <ItemStyle Font-Size="13px" Width="16%" />
                        <ItemTemplate>
                            <asp:Label ID="ThirdLabel" runat="server" ReadOnly="true" Text='<%# Eval("Col4") %>'
                                Style="text-align: left;"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Q4">
                        <ItemStyle Font-Size="13px" Width="16%" />
                        <ItemTemplate>
                            <asp:Label ID="FourthLabel" runat="server" ReadOnly="true" Text='<%# Eval("Col5") %>'
                                Style="text-align: left;"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Q5">
                        <ItemStyle Font-Size="13px" Width="16%" />
                        <ItemTemplate>
                            <asp:Label ID="FifthLabel" runat="server" ReadOnly="true" Text='<%# Eval("Col6") %>'
                                Style="text-align: left;"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EditRowStyle BackColor="#EBEBEB" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" Height="10px" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>

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[i].Split(',').Take(fileContent[i].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[i];
            dr["Col1"] = row_names[i];
            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();
    }

the for loop inside ReadToEnd(string filePath) function

 for (int i = 0; i < columns.Count() - 1; i++)
 {
   dtDataSource.Columns.Add("Col" + (i + 1));
 }

it should read as

 for (int i = 0; i < columns.Count(); i++)
 {
    dtDataSource.Columns.Add("Col" + (i + 1));
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM