Datagrid Items Dynamic Control

otherside

Centurion
Joined
Mar 16, 2003
Messages
127
Location
UK - Greece
Hi guys,

I need some help with the datagrids. I've spent all day with books forums etc. but i can't find a solution that works.

How is it possible to control the format of every item based on data from a dataset. For example say i have a field called "Result", what i need is if this field is 0 the row to have a background color red if it is 1 green if 2 blue. So what i need is to control some properties from data of a database. Also another example, i have a datagrid with two button columns one pass one fail, but what i need is that the records that have been evaluated not to display these to buttons.

Any ideas or code examples are welcome.

Thanks
 
In your itemdatabound event do..

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

Select Case e.Item.DataItem("Result")
Case 0
e.item.backcolor = 'your color
'or
e.item.cssclass = 'your style sheet class
Case else ....
End Select

Dim myButton As Button = e.item.Findcontrol("btnAdd")
myButton.Visible = False

End If
 
Thanks a lot that worked,

it's very similar to what i've tried up until now,

i have a question though

if that works fine on the itemboundevent

why this doesn't work ?

Code:
 TextBox1.Text = DataGrid1.Items.Item(1).DataItem("NName")

i get a System.NullReferenceException: Object variable or With block variable not set. exception

Thanks
 
Is this line inside your itemdatabound or outside it? Can I see your whole itemdatabound event code? From the looks of it, if that is inyour itemdatabound, then this part

DataGrid1.Items.Item(1).DataItem("NName")

You are referring to the item index, which is 1, so you are setting the text of each row to the datagrids 2nd row's dataitem. Now if the itemindex of your databound event is other than 1, then I dont think you can access the DataItem any longer. Use

Textbox.text = e.item.dataitem("NName")

this will use the current row's data
 
Back
Top