[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
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
Re: colour row datagrid on constraint
so for a check box in will it also be 0 or 1
i
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
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
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
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
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
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
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
Re: colour row datagrid on constraint
what line is it crashing on ? The text should work fine.
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
Re: colour row datagrid on constraint
can you post your html and vb code?
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