|
-
Oct 4th, 2005, 08:36 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] colour row datagrid on constraint
colour row in datagrid
hi there i want to colour my rows in my datagrid differently if a certain field is true
i.e.
if this column = 1 then
dgMsgIn.Items(e.Item.ItemIndex).BackColor = Color.Red
endif
does anyone know how to write such an event
thanks
it works 60% of the time, all the time.
-
Oct 5th, 2005, 09:58 AM
#2
Addicted Member
Re: colour row datagrid on constraint
You can do this in your itemdatabound event of the datagrid control, like so:
VB Code:
Private Sub dgrMyGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles MyGrid.ItemDataBound
If Not e.Item.ItemType = ListItemType.Header Or Not e.Item.ItemType = ListItemType.Footer _
Or Not e.Item.ItemType = ListItemType.Pager Then
Select Case e.Item.Cells(2).Text
Case "0"
e.Item.BackColor= Color.Violet
Case "1"
e.Item.BackColor= Color.Blue
End Select
End If
End Sub
-
Oct 5th, 2005, 10:48 AM
#3
Thread Starter
Fanatic Member
Re: colour row datagrid on constraint
so for a check box in will it also be 0 or 1
i
it works 60% of the time, all the time.
-
Oct 5th, 2005, 06:39 PM
#4
Addicted Member
Re: colour row datagrid on constraint
A checkbox is slightly different. You'll need to find the control in the item first, and then use a select case to see what value it is.
VB Code:
Dim chkBox as CheckBox = CType(e.Item.FindControl("chkMyCheckbox"), CheckBox)
Select Case chkBox.Checked
Case True
e.Item.BackColor= Color.Violet
Case False
e.Item.BackColor= Color.Blue
End Select
-
Oct 6th, 2005, 04:32 AM
#5
Thread Starter
Fanatic Member
Re: colour row datagrid on constraint
you wouldnt know how to create this checkbox column in the html area, asp vb
i had a column in before but it didnt seem to work
i think i have to code the checkbox in
it works 60% of the time, all the time.
-
Oct 6th, 2005, 04:42 AM
#6
Thread Starter
Fanatic Member
Re: colour row datagrid on constraint
have this so far
HTML Code:
asp:TemplateColumn HeaderText = "Sent">
<ItemTemplate>
<asp:CheckBox ID = "chkMyCheckBox" Enabled="false" runat="server" />
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
i also get this error then when using the code to colour the checkbox
object reference not set not reference .....
i think im naming them wrong
Last edited by d2005; Oct 6th, 2005 at 04:52 AM.
it works 60% of the time, all the time.
-
Oct 6th, 2005, 04:49 AM
#7
Thread Starter
Fanatic Member
Re: colour row datagrid on constraint
can i link my check box now to a column in swl table,
i will make anew column called checked,
then if column checked = 1 colour this way
else
colour this way
how can i link my chaeckbox to the datagrid values
i.e. select this from there
if its 1 then checkbox is checked
then that colour coding can do the work for colours
it works 60% of the time, all the time.
-
Oct 27th, 2005, 05:14 AM
#8
Thread Starter
Fanatic Member
Re: colour row datagrid on constraint
ok im back to this problem now and im attacking it from a new angle
my datagrid is coded in the html
i want to use this code to colour my datarows
Private Sub dgrMyGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles MyGrid.ItemDataBound
If Not e.Item.ItemType = ListItemType.Header Or Not e.Item.ItemType = ListItemType.Footer _
Or Not e.Item.ItemType = ListItemType.Pager Then
Select Case e.Item.Cells(5).Text
Case "N"
e.Item.BackColor= Color.Violet
Case "C"
e.Item.BackColor= Color.Blue
End Select
End If
End Sub1
column 5 is selected from the database and conatains characters
N C example
at the moment im getting an error
Specified argument was out of the range of valid values. Parameter name: index
Last edited by d2005; Oct 27th, 2005 at 05:36 AM.
it works 60% of the time, all the time.
-
Oct 27th, 2005, 05:43 AM
#9
Addicted Member
Re: colour row datagrid on constraint
Is it crashing on this line?
VB Code:
Select Case e.Item.Cells(5).Text
If so, your index (5) is wrong. Remember columns start at index 0, so maybe it should be
VB Code:
Select Case e.Item.Cells(4).Text
-
Oct 27th, 2005, 05:54 AM
#10
Thread Starter
Fanatic Member
Re: colour row datagrid on constraint
the index is fine
ive been messing around testing etc...
i have another column where the text is a number
that works fine
but when i try to do it using the characters it doesnt
could that be the problem
the index has been resolved
it works 60% of the time, all the time.
-
Oct 27th, 2005, 06:14 AM
#11
Addicted Member
Re: colour row datagrid on constraint
what line is it crashing on ? The text should work fine.
-
Oct 27th, 2005, 06:20 AM
#12
Thread Starter
Fanatic Member
Re: colour row datagrid on constraint
i know it should work its very strange, i even changed it to an if statement
that also works with the column that holds a number
but it doesnt seem to work with the column i want to use with characters
the app does not crash at all, it just doesnt color
nightmare
it works 60% of the time, all the time.
-
Oct 27th, 2005, 06:30 AM
#13
Addicted Member
Re: colour row datagrid on constraint
can you post your html and vb code?
-
Oct 27th, 2005, 06:32 AM
#14
Thread Starter
Fanatic Member
Re: colour row datagrid on constraint
thanks buddy got it working
i looked in my db,
it is 2 characters long
so "N "
works fine
thanks
it works 60% of the time, all the time.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|