【问题标题】:Binding a text to a Itemtemplate in a Gridview using c#使用c#将文本绑定到Gridview中的Itemtemplate
【发布时间】:2016-04-25 10:46:48
【问题描述】:

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

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:label 区域中显示过滤后的字符串。我应该使用什么命令?

【问题讨论】:

  • 你是从DataTable..填充gridview吗?
  • 是的!来自 SQL 源
  • 既然你有 List 你想在gridview中显示,对吧..??
  • 你可以使用Eval
  • 您可以在DataTable 中添加一列并根据行,只需用您的字符串填充新列,然后绑定BoundField 通常的方式。

标签: c# asp.net gridview itemtemplate


【解决方案1】:

我假设你的 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>
       }
   }

希望这会有所帮助

【讨论】:

  • 很高兴我能提供帮助..请检查它作为接受的答案:)
猜你喜欢
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 2017-06-14
  • 1970-01-01
  • 2013-07-30
  • 2016-09-20
  • 2013-04-29
  • 1970-01-01
相关资源
最近更新 更多