DataGrid Question

A

AD

I have a datagrid which I have bound to data set.
One of the cols in the datagrid( dataset) is a boolean
I want to make the backcolor of the row in datagrid to a certain color
when value of the boolean is true.
is there any easy way to do that.

here is what is in my html (aspx)
<asp:datagrid ...>
<asp:BoundColumn DataField="bIsPaid" HeaderText="Paid?" />
....


I am binding datasource in page load.

I will appreciate any replies.
 
S

Stevie_mac

You know, this question is answered tons of times (in fact 3 messages down is
one very similar message!)
Any way...
Handle DataGrid OnItemDataBound(...) event & test the value of the boolean
field. If its true, change the colour - eg...

This example assumes the CheckBox is in Cell 0 of each row.

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If e.Item.ItemIndex < 0 Then Return
Dim oCheck As CheckBox
oCheck = CType(e.Item.Cells(0).Controls(1), CheckBox)
If oCheck.Checked Then
e.Item.Cells(0).BackColor = Color.Red
End If
End Sub

One thing to note - see "e.Item.Cells(0).Controls(1)" - the Cell(0).Controls(0)
will probably be a literal (SPAN) so Controls(1) should get your CheckBox (I am
assuming you used a templated column & added the checkbox in the Tempate
Editor). If not, you can pause your code & 'QuickWatch' e.Cells(0).Controls(x)
to find your control

Good luck - Stevie_mac
 
A

AD

thanks steve but here is my problem:

i have the following code:

<asp:TemplateColumn HeaderText="Recurring?">
<ItemTemplate>
<span>
<%# ( DataBinder.Eval( Container.DataItem,"Recurring","{0:s}")) ==
"True" ? "Yes":"No"%>
</span>
</ItemTemplate>


In case the value is No I want to paint bg red.


I am not able to access the text in this case
 
S

Stevie_mac

I am not able to access the text in this case
Why not?
If you handle the OnItemDataBound event, you can access the text from
e.Item.DataItem("Recurring")
so try ...
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
myDataGrid.ItemDataBound
If e.Item.ItemIndex < 0 Then Return
If e.Item.DataItem("Recurring") = "True" Then
e.Item.Cells(0).BackColor = Color.Red
End If
End Sub

(Yes i know its VB - but C# is almost identical - you can re-write it!)
 
A

AD

Well, dataitem is NOT a collection.
I am not sure how you are using
e.Item.DataItem("Recurring")
??
 
A

AD

This one is for your Sonali.NET [MVP]
Its good to be a MVP and also good to advertise your article but
sometimes its good to take a look at the question being asked before
answering. no offense
 
S

Stevie_mac

Great - but watch out for Nulls in "Recurring" field! (i'm sure your SQL WHERE
clause prevents Query returning nulls in the "Recurring" field - since you are
not handling them here!)

To handle nulls simply, append "" before .ToString (you can, can't you? goddam,
my C# is rusty, i'm starting to doubt stuff - to much VB not enough C/C++/C# -
ah if only they let up use the 'hard stuff' at work - sigh)


AD said:
Well this is what worked. Thanks Stevie_mac for your help

if(((DataRowView ) (e.Item.DataItem))["Recurring"].ToString ().ToLower() != "true" )
{
e.Item.BackColor = Color.LightSalmon ;
}
thanks again for your help





"Stevie_mac" <[email protected]> wrote in message
OK, from all you have written, i've assumed there is a field in your source data
called "Recurring"

Therefore, when you bind your data grid, you can access
e.Item.DataItem("Recurring") IN OnDataItemBound. Therfore if you can access it,
you can test it - meaning you can do your conditional formating.

Trust me!

Tell you what, put a pause on a place in OnItemDataBound & callup Quickwatch.
Paste in e.Item.DataItem(0) & see what you get. It will be the data (1st
ordinal ) from your source data. It you do have a data field called Recurring,
put e.Item.DataItem("Recurring") in quickwatch - see what you get.

PS My assumtion about "Recurring" is from the code you previously supplied - you
appear to be binding to a data field called "Recurring" - see snip below
<ItemTemplate>
<span>
<%# ( DataBinder.Eval( Container.DataItem,"Recurring","{0:s}")) ==
"True" ? "Yes":"No"%>
</span>
</ItemTemplate>

BTW, e.Item.DataItem is a System.Data.DataRowView - check it out





messages
down
is
CheckBox (I
am
 
A

AD

Well, I have a data grid and I wanted to change the whole data row bk
color to some color.anyway i have got that working but thanks...
 

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
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top