i have a bit value (Black) i want to display its status in gridview as if its true, the row display \"Yes\", otherwise the row display \"No\", this is my code, but the resul
I don't know your datasource, but if you can evaluate it, do something like this:
<asp:TemplateField HeaderText="Status">
<ItemStyle CssClass="list" />
<ItemTemplate>
<%# GetBit(Eval("BlackBit"))%>
</ItemTemplate>
</asp:TemplateField>
And code-behind:
private string GetBit(object objBit)
{
if (Convert.ToInt32(objBit) == 1)
{
return "Yes";
}
return "No";
}
Do you need to iterate through a DataTable dt on each RowDatabound ?
If you do not need this could you try:
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Boolean bitBlack = Convert.ToBoolean(e.Row.Cells[7].Text);
if (bitBlack)
{
e.Row.Cells[7].Text = "Yes";
}
else
{
e.Row.Cells[7].Text = "No";
}
}
}
You could always use the rows DataItem to get the underlying DataSource
:
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
bool isBlack = row.Field<bool>("Black");
e.Row.Cells[7].Text = isBlack ? "Yes" : "No";
}
}