Format datagrid in code behind

P

Poppy

In my code behind I add a coloumn to a datgrid :

Code:
lBlockUsedUp.DataField = "BlockUsedUp"
Code:
lBlockUsedUp.HeaderText = "Blocks Used Up"
Code:
lBlockUsedUp.ItemStyle.Width = New Unit(300)
Code:
 dgBiopsyLocation.Columns.Add(lBlockUsedUp)

I then bind in code with :

Code:
 dgBiopsyLocation.DataSource = iDataset.Tables
("ArrBiopsyLocation")
Code:
dgBiopsyLocation.DataBind()

The problem I am having is that the field "BlockUsedUp",
which is a bit field in SQL SERVER always shows "FALSE"
when the value = 0. I want this to be "" if it is 0 and I
want to do it all in my code behind.

Can anyone help ?
 
E

Eliyahu Goldin

You can do it in either ItemDataBound or PreRender event.

In ItemDataBound you get a reference to an item. Check if it is a data item,
as opposed to a header item. The data item contains property Cells, which is
a collection of the column. You can get the value of the column you need to
format and set it to the formatted value.

In PreRender event you can loop through all data rows and do the same.

Eliyahu
 
S

Scott G.

An alternate approach that doesn't involve a bunch of code would be to add a "virtual" column to your table, so after you read the dataset you could do the following (just off the top of my head, see the docs for the correct syntax)

iDataset.Tables["ArrBiopsyLocation"].Columns.Add(new DataColumn("SBlockUsedUp", typeof(string), "IFF(BlockUsedUp = 0, ' True', ' ')"));

Then change the column DataField to "SBlockUsedUp"

Scott
You can do it in either ItemDataBound or PreRender event.

In ItemDataBound you get a reference to an item. Check if it is a data item,
as opposed to a header item. The data item contains property Cells, which is
a collection of the column. You can get the value of the column you need to
format and set it to the formatted value.

In PreRender event you can loop through all data rows and do the same.

Eliyahu
 

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

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top