2014-02-26 34 views
2

我有兩個選項卡的TabContainer的:導入CSV文件導入到現有的GridView

  • 第一個標籤包含一個按鈕。
  • 第二個選項卡包含一個具有三個文本框的gridview。

我想做到以下幾點:當一個人點擊在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; 
    } 
+0

是否有錯誤信息? –

+0

您是否嘗試過暫時禁用更新面板?您可以通過在頁面上註釋它們或通過在腳本管理器上將EnablePartialRendering屬性設置爲false來全局禁用它們。 – julealgon

回答

-2

你的代碼,如果你使用不應該給任何問題: fileContent.Length代替fileContent.Count()

+0

爲什麼不呢?請解釋一下,否則這不是一個答案。 –

+0

這顯然是錯誤的。使用計數在這裏產生相同的結果。 – julealgon

+0

我認爲C#中不存在string.count()方法,請在Visual Studio C#程序中嘗試以下操作:1.聲明一個字符串變量2.檢查點上的可用函數和屬性。我沒有看到count(),它給出了一個錯誤。而長期完美的作品。 – Neha