Search code examples
c#asp.netgridviewitemtemplate

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


I have a problem, i have a method using c# where i read in a long text which contains a pathway to a folder. And by breaking it down using regex i save a small text which i want to show in my gridview. The code looks like this

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);
            }
        }

And the gridview looks like this:

<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>

I want to show the filteredsting in the asp:label area where i wrote "what should i write here". What command should i use?


Solution

  • I assume you have That string in your Gridview Row on aspx, Then add OnRowDataBound="GridView1_OnRowDataBound" Event on gridview

    and then try this code on code behind

    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>
           }
       }
    

    Hope This will help