Formating a Particular Cell

L

Larry Dodd

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.
 
K

Ken Cox [Microsoft MVP]

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?
http://www.microsoft.com/security/security_bulletins/ms03-026.asp



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.
 
S

Stevie_mac

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)
 
S

Stevie_mac

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.
 
L

Larry Dodd

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,135
Messages
2,570,786
Members
47,342
Latest member
KelseyK737

Latest Threads

Top