Convert part of string into Hyperlink in ASP.Net GridView BoundField using Regular Expression

Mehram
 
on Mar 29, 2021 11:50 PM
Sample_856254.zip
2161 Views

I have a GridView on page with BoundField Column name is "Name detail"

GridView showing result like this

"MUHAMMAD SHAKIR / MUHAMMAD YAKOOB (0002-000593) / JUNAGARH WALA"

Need to show () value as a LinkButton to open detail of Muhammad Yakoob on click.

Only the brackets value as a link (0002-000593) enabling us to open something on click.

Please guide.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 31, 2021 12:32 AM
on Jan 06, 2022 09:26 AM

Hi Mehram,

Check this example. Now please take its reference and correct your code.

Note: For this sample i have used temporary DataTable. For more details refer How to create Temporary Table in ASP.Net using C# and VB.Net.

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name Detail" ItemStyle-Width="250" />
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;
using System.Text.RegularExpressions;

VB.Net

Imports System.Data
Imports System.Text.RegularExpressions

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Rows.Add("MUHAMMAD SHAKIR / MUHAMMAD YAKOOB (0002-000593) / JUNAGARH WALA");
        dt.Rows.Add("MUHAMMAD SHAKIR / MUHAMMAD YAKOOB (0002-000594) / JUNAGARH WALA");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[0].Text = Regex.Replace(e.Row.Cells[0].Text, "\\([^()]*\\)", delegate (Match match)
        {
            return string.Format("(<a href='Details.aspx?Id={0}'>{0}</a>)", match.Value.Replace("(", "").Replace(")", ""));
        }, RegexOptions.IgnoreCase);
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add("Name")
        dt.Rows.Add("MUHAMMAD SHAKIR / MUHAMMAD YAKOOB (0002-000593) / JUNAGARH WALA")
        dt.Rows.Add("MUHAMMAD SHAKIR / MUHAMMAD YAKOOB (0002-000594) / JUNAGARH WALA")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub

Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Cells(0).Text = Regex.Replace(e.Row.Cells(0).Text, "\([^()]*\)",
            Function(ByVal match As Match) String.Format("(<a href='Details.aspx?Id={0}'>{0}</a>)",
            match.Value.Replace("(", "").Replace(")", "")),
            RegexOptions.IgnoreCase)
    End If
End Sub

Screenshot