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

Row Formatting based on data in Dataset

Status
Not open for further replies.

AndyFreeman

IS-IT--Management
Mar 22, 2004
70
GB
Hello All

I a currently going through the pains of re-writing my ASP Pages through into ASP.Net.

The particular element of one application i have become stuck on is the following.

We have a call logging system to record helpdesk problems. As part of the dataset i hold a field that calculates how long teh call has been open for.
To highlight calls that have been open longer than 3 hours in ASP i changes the row colour by using an IF statement.

Can any1 suggest a way of formatting datagrids in this way or a better method maybe. As i have not really done a lot of Datagrid formatting work.

Thanks in advance

Andy
 
Consider this:

A TemplateColumn in your DataGrid containing a label, lblLengthOfCall, which would hold this value.

You may then capture that value in your OnItemDataBound event of your DataGrid and change the backcolor of the row.
Code:
~~
<TemplateColumn>
  <ItemTemplate>
    <asp:Label id=lblLengthOfCall runat=server Text='<%# ((DataRowView)Container.DataItem)["lengthOfCall"] %>' />
  </ItemTemplate>
</TemplateColumn>
~~
Code:
~~
protected void OnDGItemDataBound(object sender, DataGridItemEventArgs e)
{
  switch(e.Item.ItemType)
  {
    //We only want to do this if we are on an Item or AlternatingItem, 
    //as opposed to EditItem, Header, or Footer
    case ListItemType.Item:
    case ListItemType.AlternatingItem:

      //First, we need to grab the lengthOfCallValue
      int lengthOfCall;
      try
      {
        Label lblLengthOfCall = (Label)e.Item.FindControl("lblLengthOfCall");
        lengthOfCall = int.Parse(lblLengthOfCall.Text);
      }//try
      catch
      {
        lengthOfCall = -1;
      }//catch

      //Then, we can do something with it
      if(lengthOfCall > 3)
      {
        //e.Item refers to the row, or <tr>
        //It also contains a cell collection, so you can
        //  do these types of operations on indiv. cells, too
        e.Item.BackColor = System.Drawing.Color.Red;
      }//if
      break;
  }
}
~~

Hope this helps.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thanks for your reply.

Note to self. State i am using VB next time

I will do my best to pick out bits.

Again thanks 4 taking the time to reply to me

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top