By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 2,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

Can you customize individual data items in a DataGrid?

P: n/a
The description given in "Customizing Items Dynamically in the DataList or
DataGrid Web Server Control"
(http://msdn.microsoft.com/library/de...-us/dv_vbCode/
html/vbtskCodeCustomizingItemsDynamicallyInDataListWebC ontrolVisualBasic.asp
) says "The examples use the DataList control, but apply to the DataGrid
control as well". From what I've experienced, however, that claim isn't
quite valid, at least in the case (the one I'm concerned about) where the
customization depends on the data values (e.g. how you want to display a
given column's contents depends on what the value of the column is for that
record).

Here is the example given:
Private Sub DataList1_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
Handles DataList1.ItemDataBound
Dim dob As DateTime
Dim doblabel As Label
doblabel = CType(e.Item.FindControl("Label1"), Label)
dob = CType(doblabel.Text, DateTime)
If dob.Month = Today.Month Then
e.Item.BackColor = Color.Yellow
End If
End SubHowever, as far as I can see you can't use this method with a
DataGrid. Ina DataGrid, the Item property of "e" is of type DataGridItem,
which corresponds to an individual record/row in the table. From what I can
see, the column values of theDataGridItem are accessed through the Cells
property.
http://www.datawebcontrols.com/faqs/...tents.shtmlhas
an example where a column's field value is retrieved by a call to
"e.Item.Cells(n).Text" .The catch is that in that example, "e" is of type
DataGridCommandEventArgs, while "e" in the ItemDataBound event for the
DataGrid is of type DataGridItemEventArgs.I have found that within the
ItemDataBound method, a call to e.Item.Cells(n).Text returns the *name* of
the data field, not its value.The DataGridItem does have a Controls
property, but these controls don't even havea Name property, so the
FindControl call used above produces an error.Does anyone know of a way to
get this to work for a DataGrid?Bob Rosen


Jul 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.