2016-04-25 54 views
0

我有一個問題,我有一個使用c#的方法,其中我讀取包含通往文件夾的長文本的長文本。並通過使用正則表達式分解我保存我想在我的GridView中顯示的小文本。該代碼看起來像這樣使用C在GridView中將文本綁定到Itemtemplate#

string folder = @"X:\05_General\T\TestVehicleInfo\Vehicles\Bordeaux_2099908\Readouts\All\20160126_22138km_RESF_Tw1602\After\XCOM\Bordeaux_2099908_20160128_22159km_XCOM_ALL_DTC_CDABX1.txt"; 
     string[] parts = folder.Split('\\'); 
     List<string> filteredstrings = new List<string>(); 
     foreach (string part in parts) 
     { 
      if (Regex.IsMatch(part, @"\d{8}")) 
      { 
       filteredstrings.Add(part); 
      } 
     } 

而GridView的是這樣的:

<asp:BoundField DataField="ReadOutID" HeaderText="ReadOutID" InsertVisible="False" ReadOnly="True" SortExpression="ReadOutID" /> 
           <asp:BoundField DataField="FileName" HeaderText="FileName" SortExpression="FileName" /> 
           <asp:BoundField DataField="FileTime" HeaderText="FileTime" SortExpression="FileTime" /> 
           <asp:BoundField DataField="ImportTime" HeaderText="ImportTime" SortExpression="ImportTime" /> 
           <%-- <asp:BoundField DataField="FullPath" HeaderText="Comment" SortExpression="FullPath" />--%> 
           <asp:TemplateField HeaderText="Comment" SortExpression="FullPath"> 
            <EditItemTemplate> 
             <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FullPath") %>'></asp:TextBox> 

            </EditItemTemplate> 
            <ItemTemplate> 
             <asp:Label ID="Label1" runat="server" Text='<%# **What should i write here?** %>'></asp:Label> 


            </ItemTemplate> 

我要顯示在ASP中filteredsting:我在那兒寫過「我應該怎麼寫在這裏」的標籤區域。我應該使用什麼命令?

+0

你是從'DataTable'填充gridview ..? – KanisXXX

+0

是的!來自SQL源 –

+0

既然你有名單你想顯示到gridview,對.. ?? – KanisXXX

回答

0

我假設你已經在ASPX您Gridview行 該字符串,然後在gridview的

添加 OnRowDataBound="GridView1_OnRowDataBound"事件

,然後嘗試在這個代碼後面的代碼

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      string folder = e.Row.Cells[1].Text;//Your gridview cell location of that string 
      string[] parts = folder.Split('\\'); 
      List<string> filteredstrings = new List<string>(); 
      foreach (string part in parts) 
      { 
       if (Regex.IsMatch(part, @"\d{8}")) 
       { 
        filteredstrings.Add(part); 
       } 
      } 
      e.Row.Cells.Add(new TableCell() {Text = string.Join(",", filteredstrings)});// this will give CSV value of your List<string> 
     } 
    } 

希望這將有助於

+0

非常感謝!這解決了我的問題。 –

+0

很高興我能夠幫助..請檢查它作爲接受的答案:) – KanisXXX

相關問題