Hi comunidadmexi,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false"
OnRowDataBound="gvProducts_RowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Annotation">
<ItemTemplate>
<asp:Label ID="lblAnnotation" runat="server" Text='<%# Eval("tannotation") %>' Visible="false"></asp:Label>
<asp:TextBox ID="txtAnnotation" runat="server" Text='<%# Eval("tannotation") %>' Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("tannotation")
});
dt.Rows.Add(1, "Product 1", "ready");
dt.Rows.Add(2, "Product 2", "other");
gvProducts.DataSource = dt;
gvProducts.DataBind();
}
}
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (((DataRowView)e.Row.DataItem).Row["tannotation"] != null)
{
if (((DataRowView)e.Row.DataItem).Row["tannotation"].ToString().Contains("ready"))
{
(e.Row.FindControl("txtAnnotation") as TextBox).Visible = true;
}
else
{
(e.Row.FindControl("lblAnnotation") as Label).Visible = true;
}
}
else
{
(e.Row.FindControl("lblAnnotation") as Label).Visible = true;
}
}
}
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.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("tannotation")})
dt.Rows.Add(1, "Product 1", "ready")
dt.Rows.Add(2, "Product 2", "other")
gvProducts.DataSource = dt
gvProducts.DataBind()
End If
End Sub
Protected Sub gvProducts_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If CType(e.Row.DataItem, DataRowView).Row("tannotation") IsNot Nothing Then
If CType(e.Row.DataItem, DataRowView).Row("tannotation").ToString().Contains("ready") Then
TryCast(e.Row.FindControl("txtAnnotation"), TextBox).Visible = True
Else
TryCast(e.Row.FindControl("lblAnnotation"), Label).Visible = True
End If
Else
TryCast(e.Row.FindControl("lblAnnotation"), Label).Visible = True
End If
End If
End Sub
Screenshot