Results 1 to 14 of 14

Thread: [RESOLVED] colour row datagrid on constraint

  1. #1

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Resolved [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.

  2. #2
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: colour row datagrid on constraint

    You can do this in your itemdatabound event of the datagrid control, like so:

    VB Code:
    1. Private Sub dgrMyGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles MyGrid.ItemDataBound
    2.       If Not e.Item.ItemType = ListItemType.Header Or Not e.Item.ItemType = ListItemType.Footer _
    3.           Or Not e.Item.ItemType = ListItemType.Pager Then
    4.           Select Case e.Item.Cells(2).Text
    5.               Case "0"
    6.                   e.Item.BackColor= Color.Violet
    7.               Case "1"
    8.                   e.Item.BackColor= Color.Blue
    9.           End Select
    10.       End If
    11. End Sub

  3. #3

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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.

  4. #4
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    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:
    1. Dim chkBox as CheckBox = CType(e.Item.FindControl("chkMyCheckbox"), CheckBox)
    2. Select Case chkBox.Checked
    3.      Case True
    4.              e.Item.BackColor= Color.Violet
    5.      Case False
    6.              e.Item.BackColor= Color.Blue
    7. End Select

  5. #5

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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.

  6. #6

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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.

  7. #7

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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.

  8. #8

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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.

  9. #9
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: colour row datagrid on constraint

    Is it crashing on this line?

    VB Code:
    1. 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:
    1. Select Case e.Item.Cells(4).Text

  10. #10

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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.

  11. #11
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: colour row datagrid on constraint

    what line is it crashing on ? The text should work fine.

  12. #12

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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.

  13. #13
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: colour row datagrid on constraint

    can you post your html and vb code?

  14. #14

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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
  •  



Click Here to Expand Forum to Full Width