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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid custom row colors

Status
Not open for further replies.

tman24m

Programmer
May 21, 2001
93
US
Hello,

I'm trying to create a datagrid which has a different color for the row when the group breaks. In the second column of the datagrid I'm inserting a '-1' to flag me that is the row that needs to have a different color. Here is a sample of my grid

Document Message Type Message
000000 INFO DROP SHIP ORDER OPENED FOR ORDER NUMBER 2720
000000 INFO DROP SHIP ORDER OPENED FOR ORDER NUMBER 4615
000000 -1
010038 INFO D/S ORDER CANCELED FOR ORDER NUMBER 5724
010038 INFO DROP SHIP ORDER OPENED FOR ORDER NUMBER 5724

I want the rows with '-1' in the second column to be blue. Here is my code so far

Private Sub BindData(ByVal sortExpr As String)
Dim connStr As String = "ConnectionString"
Dim myConnection As New SqlConnection(connStr)
Dim strSQL As String = "SQL Statement"
Dim myCommand As New SqlCommand(strSQL, myConnection)
myConnection.Open()
dgOrders.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
dgOrders.DataBind()

End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
BindData("EOFDOC")
End If
End Sub

Sub SortData(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs)
BindData(e.SortExpression)
End Sub

Please help, Thanks!
 
Try something like this...
======================================
Sub dgOrders_ItemCreated(ByVal Sender As Object, ByVal e As DataGridItemEventArgs) Handles dgOrders.ItemCreated

Select Case e.Item.ItemType
Case ListItemType.Item or ListItemType.AlternatingItem
Dim iNum As Integer = e.Item.ItemIndex
If e.Item.Cells(iNum).Text = "-1" Then
e.Item.BackColor = System.Drawing.Color.Red
End If
End Select

End Sub
======================================

 
Thanks, still not quite there

Returns an error

Specified argument was out of the range of valid values. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index

Error on Line 66

Line 64: Case ListItemType.Item Or ListItemType.AlternatingItem
Line 65: Dim iNum As Integer = e.Item.ItemIndex
Line 66: If e.Item.Cells(iNum).Text = "-1" Then
Line 67: e.Item.BackColor = System.Drawing.Color.Red
Line 68: End If
 
oops. try this:

Select Case e.Item.ItemType
Case ListItemType.Item or ListItemType.AlternatingItem
If e.Item.Cells(1).Text = "-1" Then
e.Item.BackColor = System.Drawing.Color.Red
End If
End Select
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top