简体   繁体   English

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

[英]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: 我试图将数据从csv导入到gridview,但是由于某些原因,出现以下错误:

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

Here is how the csv looks like: 这是csv的样子:

在此处输入图片说明

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". 我想以相同的格式显示在GridView中,并将列名和行名设置为“ Q1,” Q2“,” Q3“,” Q4“,” Q5“。

Could anyone please help? 谁能帮忙吗? Thank you in advance. 先感谢您。 PS: I am a beginner at ASP.NET PS:我是ASP.NET的初学者

Here is my code: 这是我的代码:

aspx 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 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 ReadToEnd(string filePath)函数中的for循环

 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));
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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