View some custom text in DataGrid

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

Hello.
I have a boolean field in my database (true=1, false=0), nowwhen I bind data
to my DataGrid it shows me "1" or "0", but I want while displayng this data
show some ohter text, for example if field is 1 show "Active" and if 0
"Inactive"?
How can I do this?

Than You.
 
Hi,
Check the first question in http://www.datagridgirl.com/faq.aspx.

Hello.
I have a boolean field in my database (true=1, false=0), nowwhen I bind data
to my DataGrid it shows me "1" or "0", but I want while displayng this data
show some ohter text, for example if field is 1 show "Active" and if 0
"Inactive"?
How can I do this?

Than You.
 
There are 2 possible ways to do this stuff.

first:
on grid, you should convert your column to a template column. After that,
you go (from a right click on grid), in edit template of your column. In this
step you'll see there a label in itemtemplate. On label properties go to
databidings. On text "Bindable Property" you should write in "Custom Binding
Expresion" a code like this one

((System.Data.DataRowView)Container.DataItem)["Test"].ToString() == "1" ?
"Active" : "Inactive"

if you go to HTML mode you should see something like this
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%#
((System.Data.DataRowView)Container.DataItem)["Test"].ToString() == "1" ?
"Active" : "Inactive" %>'>
</asp:Label>
</ItemTemplate>
if is not exatly like this you should change the text to be somehow like
this.

the second way is a little bit complicated
you should write some code on grid event itemcreated. Also you should have
that column as a teampte column.
Soo the code should look like this :
if ((e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==
ListItemType.Item) && e.Item.DataItem != null) {
Label b = e.Item.Cells[2].Controls[1] as Label;
int test = Convert.ToInt32(((DataRowView)e.Item.DataItem).Row["Test"]);
if(b != null){
b.Text = test == 1 ? "Active" : "Inactive";
}

Cheers HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top