Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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's as Yes.

How to do it


thanks in advance
Posted
Updated 9-Jan-11 23:05pm
v2
Comments
Dalek Dave 10-Jan-11 5:05am    
Edited for Grammar and Readability.

You can easily do it inside RowDataBound Event of Grid. Use FindControl method to find the control from Gridview and change the value.

Below is one Sample Code where Student is a sample class and Status is a field


C#
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Student s = (Student)e.Row.DataItem;
        if (s.Status == 1)
        {
            e.Row.Cells[2].Text = "Yes"
        }
        else
        {
            e.Row.Cells[2].Text = "false"
        }
    }
}


Hope this will help !!
 
Share this answer
 
Comments
Hiren solanki 10-Jan-11 4:58am    
Good answer abhijit.

you can further truncate code to

e.Row.Cells[2].Text = s.Status == 1?"Yes":"No";
Dalek Dave 10-Jan-11 5:05am    
Spot on!
Sandeep Mewara 10-Jan-11 5:29am    
Exactly. Should be accepted as answer! 5!
Hiren solanki 10-Jan-11 7:21am    
No abhijit I am not considering you as a downvoters and I don't want to make discussion about this bloody hell thing as of now I've some moral supporter with me.

If you've downvoted then I may have lost 36 points but I've lost only 16 so obviously you are not a downvoter.
You can print conditional formatted string there.

Like if it's Lable inside grid

Lable1.Text = i==0?"No":"Yes";


Hope it helps.
 
Share this answer
 
Comments
Abhijit Jana 10-Jan-11 4:50am    
You have to be more specific in which event you have to do this. Particularly when he as asking about GridView.
Hiren solanki 10-Jan-11 4:56am    
No one is here to do complete home work. I am just showing a way to do.
Sandeep Mewara 10-Jan-11 5:30am    
Looks like you have tried to add on Abhijit's answer. If so, atleast mention that so that your answer looks complete.

If you leave Abhijit's answer than your answer does not make sense at all.
Hiren solanki 10-Jan-11 5:32am    
Thanks sandeep but I've answered before abhijit.
Sandeep Mewara 10-Jan-11 5:38am    
Oh! my bad. I am sorry I missed it.

In that case, dont you think your answer made much of sense on when and where to write it? But it's just me asking!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900