将csv文件导入现有gridview

时间:2014-02-26 21:31:05

标签: c# asp.net csv gridview

我有一个带有两个标签的tabcontainer:

  • 第一个标签包含一个按钮。
  • 第二个标签包含一个包含三个文本框的网格视图。

我想执行以下操作:当单击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;
    }

1 个答案:

答案 0 :(得分:-2)

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