将数据从 csv 导入到 GridView ASP.NET/C#

问题描述 投票:0回答:1

我正在尝试将数据从 csv 导入到 gridview,但由于某种原因,我收到以下错误:

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Col5'
.

这是 csv 的样子:

enter image description here

我想以相同的格式在GridView中显示,并将列名和行名设置为“Q1,”Q2”,“Q3”,“Q4”,“Q5”。

P.S:我是 ASP.NET 的初学者。

这是我的代码:

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();
    }
c# asp.net csv gridview
1个回答
2
投票

ReadToEnd(string filePath) 函数中的 for 循环

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

它应该读作

 for (int i = 0; i < columns.Count(); i++)
 {
    dtDataSource.Columns.Add("Col" + (i + 1));
  }
© www.soinside.com 2019 - 2024. All rights reserved.