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!

OnItemDatabound and Datagrid 2

Status
Not open for further replies.

JPimp

Technical User
Mar 27, 2007
79
US
When I try to use the onitemdatabound function in a .NET page, nothing happens? I do a simple query to pull in all data from a small table (test mode still), and then use the itemdatabound to highlight some columns, but I get no datagrid, no error, nothing?

What Am I doing wrong other than amateur coding,hehehe

Code:
Connection String goes here

conMetrics.Open()



cmdSelect = New OleDbCommand("Select * From Metrology order by [Date_Time] desc", conMetrics)

        Dim DataReader As OleDbDataReader


        DataGrid1.DataSource = cmdSelect.ExecuteReader
        DataGrid1.DataBind()



        If (e.Item.ItemType = ListItemType.Item Or _
        e.Item.ItemType = ListItemType.AlternatingItem) Then
            If (e.Item.Cells(3).Text) < "6.000" Then _
            e.Item.Cells(3).BackColor = System.Drawing.Color.FromName("#ffccff")
        End If

        conMetrics.Close()

Thanks for any insight

Kai-What?
 
the code above is out of context so we don't know for sure where this is happening, but if you trying to bind the grid in the itemdatabound event then it will never execute. the data should be bound on an event like Loading or a button click. the grid should have the itemdatabound event assigned to a delegate which can then handel the events of each row being bound.
Code:
void page_load(object sender, eventargs e)
{
   if(!ispostback) 
   {
      grid.datasource = GetData();
      grid.databind();
   }
}

void grid_itemdatabound(object sender, GridViewRowDataBoundEventArgs e)
{
   //do something to the row.
   // header/footer/pager have row index of -1
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
ok, this is working (almost), it highlights my whole row, rather than just the cell (Length Top XD), which is what I want, so I know I am missing something, I am treating the rows rather than cells, but I tried pointing to a cell like this:

Code:
If e.Item.ItemType <> ListItemType.Header And _
e.Item.ItemType <> ListItemType.Footer And _
e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then

  If Convert.ToDecimal(e.Item.Cells(3)) < 5.0201 Then
                    e.Item.BackColor = Color.Yellow
End If

            End If

        End If

But that did not do anything at all, the code below highlights the row where the statement is true...


Code:
Sub dlTitles_DataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)

If e.Item.ItemType <> ListItemType.Header And _
e.Item.ItemType <> ListItemType.Footer And _
e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then

            Dim LengthTopXD As Object
            LengthTopXD = DataBinder.Eval(e.Item.DataItem, "Length Top XD")

            If Not LengthTopXD.Equals(DBNull.Value) Then
                If Convert.ToDecimal(LengthTopXD) < 5.0201 Then
                    e.Item.BackColor = Color.Yellow

                    If Convert.ToDecimal(LengthTopXD) > 5.0397 Then
                        e.Item.BackColor = Color.Yellow
                    End If
                End If

            End If

        End If



Thanks again!

Kai-What?
 
e.item refers to the row. You need to specify the cell:
e.Item.Cells(0).BackColor = Color.Yellow
 
Thanks jbenson, I figured that's where it was and I tried that and it worked great!!

Kai-What?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top