Display of digits into words in gridview

Posted by Amritha444 under C# on 1/10/2011 | Points: 10 | Views : 1800 | Status : [Member] | Replies : 4
Hi all

i want to display particular field which will be always 1 or 0 in yes or no string format.when binding that table into grid.all 0's should come as no and 1 as yes.how to do it


thanks in advance




Responses

Posted by: Chvrsri on: 1/10/2011 [Member] [Moderator] [MVP] Silver | Points: 25

Up
0
Down
For this you have to change the value of a particular cell in a grid in the RowDataBound event of the gridview.

Initally the Datatable which you are binding to the gridview must be stored some where. For that i use ViewState to store that DataTable.(This is to be done at Page_Load())
In this way :

DataTable table=null;
int count=null;
if (ViewState["MulDel"] == null)
ViewState["MulDel"] = table;



Now in the row databound event change the value in the particular cell by this way :

 

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (table == null && ViewState["MulDel"] != null)
table = (DataTable)ViewState["MulDel"];
if (table != null)
{
if (table.Rows[count][3].ToString() == "1")
{
e.Row.Cells[5].Text = "GOOD";
}
else
{
e.Row.Cells[5].Text = "BAD";
}
}
count++;
}



Hope this helps !!!

Thanks,
Radha Srikanth

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: SheoNarayan on: 1/10/2011 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
As far as I understood your question, you just need to write following code

 <asp:TemplateField HeaderText="IsPublished / By">

<ItemTemplate>
<%# Eval("IsPublished").ToString().Equals("0") ? "No" : "Yes" %>
</ItemTemplate>
</asp:TemplateField>

Here, I am assuming that the field which has 0 and 1 data is "IsPublished". Write above as the Columns of the Grid and you should get your desired result.

Thanks!

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/10/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

You can use the below script proces and get ur requirement done

<asp:TemplateField HeaderText="Active" SortExpression="Active">     

<ItemTemplate><%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %></ItemTemplate>
</asp:TemplateField>


Thanks
Karthik
www.f5Debug.net

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suresh.mekkattil on: 1/10/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,
You can loop through the gridview rows and can find the cell. and by checking the value in the cell for "0" or "1".

OR

You can simply alter your Datasource say, DataTable. Which means, loop through the DataTable rows and replace the "0" with "No" and the "1"s with "Yes"

OR

You can simply use, <ItemTemplate><%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %></ItemTemplate>

-----
Suresh M

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response