Using RowDataBound
HTML:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView_OnRowDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblIsRead" Text='<%# Eval("IsRead") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Product" HeaderText="ProdcutName" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSold" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateGrid();
}
}
private void PopulateGrid()
{
DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[3]{new DataColumn("ID",typeof(int)),
new DataColumn("IsRead",typeof(bool)),
new DataColumn("Product",typeof(string))
});
dt2.Rows.Add(1, true, "Mobile");
dt2.Rows.Add(2, false, "TV");
dt2.Rows.Add(3, true, "Fridge");
this.GridView1.DataSource = dt2;
this.GridView1.DataBind();
}
protected void GridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
bool isRead = Convert.ToBoolean((e.Row.Cells[1].FindControl("lblIsRead") as Label).Text);
(e.Row.Cells[3].FindControl("btnSold") as Button).Text = isRead == true ? "true" : "false";
}
}