【问题标题】:Import csv file into existing gridview将 csv 文件导入现有的 gridview
【发布时间】:2014-02-26 21:31:05
【问题描述】:

我有一个带有两个标签的标签容器:

  • 第一个选项卡包含一个按钮。
  • 第二个选项卡包含一个带有三个文本框的网格视图。

我想执行以下操作:当单击 tab1 中的按钮时,tab2 中的 gridview 会填充预先指定的 csv 文件中的数据(请注意,该文件与 gridview 具有相同的列名)。

这是我目前所拥有的,但由于某种原因,gridview 没有填充 csv 中的数据。

aspx.cs

<asp:UpdatePanel ID="WholeUpdatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
       <asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" HeaderText=""
            Width="900px" TabStripPlacement="Top" ScrollBars="None" UseVerticalStripPlacement="false"
            VerticalStripWidth="120px" BackColor="White" BorderColor="White" 
            Style="margin-right: 84px">
            <asp:TabPanel ID="OptionPanel" runat="server" Height="600px">
                <HeaderTemplate>
                    Simulation Option
                </HeaderTemplate>
                <ContentTemplate>
                      <asp:Button ID="PreviousSimButton" runat="server" Text="Run Previous Simulation" Width="250px" OnClick="PreviousSimButton_OnClick" />
                <ContentTemplate>
   </asp:TabPanel>
 <asp:TabPanel ID="TabPanel1" runat="server" Height="600px" >
         <HeaderTemplate>
                    General
                </HeaderTemplate>
                <ContentTemplate>
                    <asp:UpdatePanel ID="TestUpdatePanel" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <asp:Panel ID="GeneralPanel" runat="server" Height="274px">
                                 <div style="overflow: auto; height: 222px; width: 100%">
                                     <asp:GridView ID="InflationGridView" runat="server" AutoGenerateColumns="False" Width="52%"
                                                    ShowHeaderWhenEmpty="True" CellPadding="4" ForeColor="#333333" GridLines="None"
                                                    AllowSorting="True" ShowFooter="True">
                                                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" Height="2px" />
                                                    <Columns>
                                                        <asp:TemplateField HeaderText="Start Year">
                                                            <ItemStyle Font-Size="13px" Width="20%" HorizontalAlign="Center" />
                                                            <ItemTemplate>
                                                                <asp:TextBox ID="StartInflationTextBox" runat="server" Width="60px" Text="" Style="text-align: center;"></asp:TextBox>
                                                                <asp:NumericUpDownExtender ID="StartInflationNumericUpDownExtender" runat="server"
                                                                    TargetControlID="StartInflationTextBox" Minimum="1" Width="60">
                                                                </asp:NumericUpDownExtender>
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="End Year">
                                                            <ItemStyle Font-Size="13px" Width="20%" HorizontalAlign="Center" />
                                                            <ItemTemplate>
                                                                <asp:TextBox ID="EndInflationTextBox" runat="server" Width="60px" Text="" Style="text-align: center;"></asp:TextBox>
                                                                <asp:NumericUpDownExtender ID="EndInflationNumericUpDownExtender" runat="server"
                                                                    TargetControlID="EndInflationTextBox" Minimum="1" Width="60">
                                                                </asp:NumericUpDownExtender>
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="Inflation Rate">
                                                            <ItemStyle Font-Size="13px" Width="25%" HorizontalAlign="Center" Height="2px" />
                                                            <ItemTemplate>
                                                                <asp:TextBox ID="InflationTextBox" runat="server" Text="" Width="60px" Style="text-align: center;"></asp:TextBox>
                                                                %
                                                            </ItemTemplate>
                                                            <FooterStyle HorizontalAlign="Right" />
                                                            <FooterTemplate>
                                                                <asp:Button ID="AddNewInflationRowButton" runat="server" Text="Add New Row" OnClick="AddNewInflationRowButton_Click"
                                                                    Height="25px" />
                                                            </FooterTemplate>
                                                        </asp:TemplateField>
                                                    </Columns>
                                                    <FooterStyle Font-Bold="True" ForeColor="White" Height="20px" />
                                                    <HeaderStyle 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>
                                            </div>
                                 </asp:Panel>
                   </ContentTemplate>
             </asp:UpdatePanel>
         </contenttemplate>
    </asp:TabPanel>
   </asp:TabContainer>
  </ContentTemplate>
 </asp:UpdatePanel>

aspx.cs

    protected void PreviousSimButton_OnClick(object sender, EventArgs e)
    {
            TabContainer1.ActiveTabIndex = 1;

            string file = "C:\\inst_research\\MonteCarlo\\Data\\inflation.csv";
            InflationGridView.DataSource = (DataTable)ReadToEnd(file);
            InflationGridView.DataBind();

            WholeUpdatePanel.Update();
            TestUpdatePanel.Update();

    } 

    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(); i++)
            {
                dtDataSource.Columns.Add(columns[i]);
            }

            for (int i = 1; i < fileContent.Count(); i++)
            {
                string[] rowData = fileContent[i].Split(',');
                dtDataSource.Rows.Add(rowData);
            }
        }
        return dtDataSource;
    }

【问题讨论】:

  • 有错误提示吗?
  • 您是否尝试过暂时禁用更新面板?您可以通过在页面上将它们注释掉或通过在脚本管理器上将EnablePartialRendering 属性设置为false 来全局禁用它们来做到这一点。

标签: c# asp.net csv gridview


【解决方案1】:

如果您使用以下代码,您的代码应该不会出现任何问题: fileContent.Length 而不是 fileContent.Count()

【讨论】:

  • 为什么不呢?请解释一下,否则这不是答案。
  • 这完全是错误的。在这里使用 Count 会产生相同的结果。
  • 我认为 C# 中不存在 string.count() 方法,请在 Visual Studio C# 程序中尝试: 1. 声明一个字符串变量 2. 检查哪些函数和属性可用点。我在那里没有看到 count() ,它给出了一个错误。而 .Length 效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
  • 2012-08-25
  • 2011-10-04
  • 2021-12-10
  • 1970-01-01
相关资源
最近更新 更多