Formating a Particular Cell

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default Formating a Particular Cell

    If I have a datagrid and I wanted to format the cell in say Row 6 Column 3
    how would I do this. I don't want to format the cell based on the data I
    just want to make the cell a different color.

    I just need to understand how to tell the datagrid I want to do something to
    a particular cell.


    Larry Dodd Guest

  2. Similar Questions and Discussions

    1. cfgrid - Update a cell based on the value of another cell
      I would like to update a hidden cell based on the value of a visible cell. Is this possible? Thanks
    2. How to set cell background based on cell value when datagrid is displayed
      I would like to check a datagrid cell value, and change the color of the cell background, when a datagrid is displayed. I want to do this as early...
    3. I need to have a tooltip appear over a cell showing data from another cell in the same row.
      I have a datagrid with locations from around the world. In hidden cells, I have the Lat and Long of that location. I need to be able to mouseover...
    4. RadioButtonList In A DataGrid Cell - Can I find the selected button without editing the cell?
      I have an ASP.NET form with a DataGrid and Button. I want to put a RadioButtonList in a DataGrid cell. I bind it to an ArrayList which has a...
    5. mouseover in cell 1, change in cell 2
      let's say i have 2 cells and what i want to create is, if i mouseover the text or image in cell 1, an image or text would appear in cell 2 ...
  3. #2

    Default Re: Formating a Particular Cell

    To change a cell in a dynamic grid, you need to catch it as it is being created
    and make the changes you want. For example, this will make the cell in row 6,
    column 3 (zero-based) blue:

    Private Sub DataGrid1_ItemCreated _
    (ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    Handles DataGrid1.ItemCreated
    If e.Item.ItemType = ListItemType.Item And e.Item.ItemIndex = 6 Then
    e.Item.Cells(3).BackColor = System.Drawing.Color.Blue
    End If
    End Sub

    Does this help?

    Ken
    MVP [ASP.NET]

    --
    Microsoft MVPs have a question for *you*: Are you patched against the Worm?
    [url]http://www.microsoft.com/security/security_bulletins/ms03-026.asp[/url]



    "Larry Dodd" <[email protected]> wrote in message
    news:[email protected]..
    If I have a datagrid and I wanted to format the cell in say Row 6 Column 3
    how would I do this. I don't want to format the cell based on the data I
    just want to make the cell a different color.

    I just need to understand how to tell the datagrid I want to do something to
    a particular cell.



    Ken Cox [Microsoft MVP] Guest

  4. #3

    Default Re: Formating a Particular Cell

    Have a look at your other post - i answered it there (ahhh what the hell - here
    it is again...)

    C'Mon man... Once you know you can handle the ItemDataBound & can access the
    current row (e) then you can do just about anything.

    Example - Check cell 3 of every row & apply formatting based on the content
    of the cell eg...
    If e.Item.Cells(3).Text.ToLower Like "*shit*" Then
    e.Item.Cells(3).ToolTip = "This Sentence contains a swear word and has
    been censored"
    e.Item.Cells(3).Text = Replace(e.Item.Cells(3).Text, "shit", "s***")
    End If


    Any hoo - heres what you want to do...

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    DataGrid1.ItemDataBound
    If e.Item.ItemIndex = 6 Then
    e.Item.Cells(3).BackColor = Color.Pink
    e.Item.Cells(3).ToolTip = "This ones pink cos its different!!!"
    End If
    End Sub


    But just incase your not getting this... ItemDataBound is an event that occurs
    when you call DataGrid1.DataBind. You need a handler for this event
    (DataGrid1_ItemDataBound(...)). It is called for each row of data in your grid.
    So test for the Row number (If e.Item.ItemIndex = 6 Then) then do the formatting
    on cell 3 (e.Item.Cells(3).BackColor = Color.Pink)


    "Larry Dodd" <[email protected]> wrote in message
    news:[email protected]..
    > If I have a datagrid and I wanted to format the cell in say Row 6 Column 3
    > how would I do this. I don't want to format the cell based on the data I
    > just want to make the cell a different color.
    >
    > I just need to understand how to tell the datagrid I want to do something to
    > a particular cell.
    >
    >

    Stevie_mac Guest

  5. #4

    Default Re: Formating a Particular Cell

    Just an aside note... You asked about Row 6, Cell 3 & technically the code below
    will set Cell 3 of Row 6 to Pink, but these are ZERO based indexes - so if you
    realy want the 3rd cell of the 6th DataRow then its like this...
    If e.Item.ItemIndex = 5 Then
    e.Item.Cells(2).BackColor = Color.Pink
    e.Item.Cells(2).ToolTip = "This ones pink cos its different!!!"
    End If

    Just to be absolutely clear and all that!

    Good luck - Stevie_Mac.


    "Stevie_mac" <[email protected]> wrote in message
    news:bhmbpi$chl$[email protected]..
    > Have a look at your other post - i answered it there (ahhh what the hell -
    here
    > it is again...)
    >
    > C'Mon man... Once you know you can handle the ItemDataBound & can access the
    > current row (e) then you can do just about anything.
    >
    > Example - Check cell 3 of every row & apply formatting based on the content
    > of the cell eg...
    > If e.Item.Cells(3).Text.ToLower Like "*shit*" Then
    > e.Item.Cells(3).ToolTip = "This Sentence contains a swear word and has
    > been censored"
    > e.Item.Cells(3).Text = Replace(e.Item.Cells(3).Text, "shit", "s***")
    > End If
    >
    >
    > Any hoo - heres what you want to do...
    >
    > Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    > DataGrid1.ItemDataBound
    > If e.Item.ItemIndex = 6 Then
    > e.Item.Cells(3).BackColor = Color.Pink
    > e.Item.Cells(3).ToolTip = "This ones pink cos its different!!!"
    > End If
    > End Sub
    >
    >
    > But just incase your not getting this... ItemDataBound is an event that occurs
    > when you call DataGrid1.DataBind. You need a handler for this event
    > (DataGrid1_ItemDataBound(...)). It is called for each row of data in your
    grid.
    > So test for the Row number (If e.Item.ItemIndex = 6 Then) then do the
    formatting
    > on cell 3 (e.Item.Cells(3).BackColor = Color.Pink)
    >
    >
    > "Larry Dodd" <[email protected]> wrote in message
    > news:[email protected]..
    > > If I have a datagrid and I wanted to format the cell in say Row 6 Column 3
    > > how would I do this. I don't want to format the cell based on the data I
    > > just want to make the cell a different color.
    > >
    > > I just need to understand how to tell the datagrid I want to do something to
    > > a particular cell.
    > >
    > >
    >
    >

    Stevie_mac Guest

  6. #5

    Default Re: Formating a Particular Cell

    That is perfect. I ws thinking that the itemindex property was the number of
    the cell. Now I now that it is the row that you are on. Thank you for all of
    the help. That is exactly what I needed to do.

    "Larry Dodd" <[email protected]> wrote in message
    news:[email protected]..
    > If I have a datagrid and I wanted to format the cell in say Row 6 Column 3
    > how would I do this. I don't want to format the cell based on the data I
    > just want to make the cell a different color.
    >
    > I just need to understand how to tell the datagrid I want to do something
    to
    > a particular cell.
    >
    >

    Larry Dodd Guest

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139