Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic row color in Datagrid. 1

Status
Not open for further replies.
Use the ItemDataBound event. Here's an example that changes the forecolor of a row to red if the value of "priority" is true. You can just as easily change the backcolor.
Code:
private void dg1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
			
	if(e.Item.ItemType == ListItemType.Header ||
		e.Item.ItemType == ListItemType.Footer ||
		e.Item.ItemType == ListItemType.Separator)
		return;
	object priority = DataBinder.Eval(e.Item.DataItem,"priority");
	if(priority != DBNull.Value)
	{
		if(Convert.ToBoolean(priority)== true)
			e.Item.ForeColor=Color.Red;
	}
}
 
Veep, is there any chance you could please translate this into VB.NET? It's not working for me.

Here is my code.......

For Each rvw As DataRowView In dv
Dim row As DataRow = rvw.Row
myInteger = rvw("name").IndexOf(" ")
Substring1 = rvw("name").Substring(0, myInteger)
ListBox1.Items.Add(Substring1)
If Trim(Substring1) = "b52008" Then

ListBox1.Items.Add("EQ")

End If
Next
If e.Item.ItemType = ListItemType.Header Or e.Item.ItemType = ListItemType.Footer Or e.Item.ItemType = ListItemType.Separator Then
Return
End If
If Substring1 = "b52008" Then
e.Item.ForeColor = Color.Red
End If


Steve
 
Is your code in the ItemDataBound event of the DataGrid? You need to make this happen when data becomes bound to the grid at which point you take action based on a value. I'm not sure what you're doing because it looks like you are looping through the rows of a DataView. In reality you need to be looping through the rows of the DataGrid. Can you show the whole Sub?
 
Sure, here it is. Thank you for your time.

Private Sub C1WebGrid1_ItemDataBound(ByVal sender As Object, ByVal e As C1.Web.C1WebGrid.C1ItemEventArgs) Handles C1WebGrid1.ItemDataBound
' Dim dv As DataView = New DataView(PhysicalDataRead())
Dim Sub1, Sub2 As String
Dim myInteger As Integer
Sub2 = "111"
For Each rvw As DataRowView In dv
Dim row As DataRow = rvw.Row
myInteger = rvw("name").IndexOf(" ")
Sub1 = rvw("name").Substring(0, myInteger)

ListBox1.Items.Add("Substring1 = " & Sub1)

If Sub1 = Sub2 Then
ListBox1.Items.Add("EQ")
End If
Sub2 = Sub1
Next


'If e.Item.ItemType = ListItemType.Header Or e.Item.ItemType = ListItemType.Footer Or e.Item.ItemType = ListItemType.Separator Then
' Return
'End If
'If Sub1 = "b52008" Then
' e.Item.ForeColor = Color.Red
'End If
End Sub

Steve
 
Here's a useful link that shows you how to change the background colour (it's basically conditional formatting):


Is basically the same as Veep's original response though but it may give you a few pointers (also you may want to elaborate on "It's not working for me" as that doesn't give us any clue's on what may be wrong!)


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I think that this is in the VB neighborhood although untested. My best guess at the moment.
Code:
Private Sub C1WebGrid1_ItemDataBound(ByVal sender As Object, ByVal e As C1.Web.C1WebGrid.C1ItemEventArgs) Handles C1WebGrid1.ItemDataBound

Dim Sub1, Sub2 As String
Dim myInteger As Integer

If e.Item.ItemType <> ListItemType.Header Or e.Item.ItemType <> ListItemType.Footer Or e.Item.ItemType <> ListItemType.Separator Then

   myInteger = (DataBinder.Eval(e.Item.DataItem,"name").ToString().IndexOf(" "))
   Sub1 = (DataBinder.Eval(e.Item.DataItem,"name").ToString().Substring(0, myInteger))

   If Sub1 = "b52008" Then
       e.Item.ForeColor = Color.Red
   End If
End If

End Sub
 
stfarm,

Don't be phased by C# code - there really isn't much difference. Here's the code from the link I gave you in both C# and VB - have a look through and see what the differences are as maybe this will help you in the future.

C#
Code:
private void OnNWDataBound(object sender, 
             System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item ||
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView rv = (DataRowView)e.Item.DataItem;
        // Get fourth column value.
        Int32 nUnitsInStock = Convert.ToInt32(rv.Row.ItemArray[4]);
        if (nUnitsInStock < 20)
        {
           e.Item.Cells[4].BackColor = Color.Red;
        }
    }
}

VB
Code:
    Private Sub OnNWDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            Dim rv As DataRowView = CType(e.Item.DataItem, DataRowView)
            Dim nUnitsInStock As Int32 = Convert.ToInt32(rv.Row.ItemArray(4))
            If nUnitsInStock < 20 Then
                e.Item.Cells(4).BackColor = Color.Red
            End If
        End If

    End Sub

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top