使用c#将文本绑定到Gridview中的Itemtemplate

Binding a text to a Itemtemplate in a Gridview using c#

我有一个问题,我有一个使用 c# 的方法,我在其中读取包含文件夹路径的长文本。通过使用正则表达式分解它,我保存了一个小文本,我想在我的 gridview 中显示它。代码看起来像这样

string folder = @"X:_General\T\TestVehicleInfo\Vehicles\Bordeaux_2099908\Readouts\All160126_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>

我想在我写 "what should i write here" 的 asp:label 区域显示过滤字符串。我应该使用什么命令?

我假设你的 Gridview 行中有那个字符串 在 aspx 上,然后在 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>
       }
   }

希望这会有所帮助