how to customize datagrid row based on data?

I am binding a table and a datagrid. 

The table has, amongst other things, a boolean field which will be false if the data in the row is in error.

When this happens, the row should get a red background (or bold text or whatever), and the accompanying text field must become read-only; ie. the user is not allowed to edit the text if it is in error.

How can I accomplish this

I have found the following example which seems only to apply to the web version. I have found nothing similar for the windows version.

***sample code for asp.net***
Answer: Handle this in the ItemDataBound event, where you can access the DataItem as it is bound to the Datagrid, and the Cells() collection of the current Datagrid item.

Sub Datagrid1_ItemDataBound(source As Object, e As DataGridItemEventArgs)
  If (e.Item.ItemType = ListItemType.Item Or _
    e.Item.ItemType = ListItemType.AlternatingItem) Then
    If Convert.ToDateTime(e.Item.Cells(1).Text) < DateTime.Today Then _
    e.Item.Cells(1).BackColor = System.Drawing.Color.FromName("#ffccff")
    If e.Item.DataItem("UserID") = 590 Then _
    e.Item.Cells(2).BackColor = System.Drawing.Color.DeepPink
  End If
End Sub



Answer this question

how to customize datagrid row based on data?

  • bhan

    (edited)
  • Shilps

    Here's a link to a recent thread that has great examples of controling the format of a cell based on it's value:

    http://www.windowsforms.com/Forums/ShowPost.aspx tabIndex=1&tabId=41&PostID=23681

  • Blacata

    This might help:

    http://msdn.microsoft.com/msdnmag/issues/02/02/cutting/

    Tony

  • jpvalappil

    Bah, sorry, I just read your post closer...  The thread I pointed to is about column formatting...

    Although it might be feasible to create a columnstyle for each column that checks for the row error and does what you want from there...

  • how to customize datagrid row based on data?