Team LiB
Previous Section Next Section

Using DataViews to Control the Display of a DataGrid

The DataGrid ItemDataBound event can be used to define the view of data that is to be displayed in a DataGrid. The following is an example of implementing this.

Code Example: Using DataViews to Control the Display of a DataGrid
Start example
private void UseDataViewToControlGrid(object sender,

                    System.Web.UI.WebControls.DataGridItemEventArgs e)
{
           if(e.Item.ItemType == ListItemType.Header)
           {
                  e.Item.Cells[0].Text = "Age";
                  e.Item.Cells[1].Text = "Labor";
                  e.Item.Cells[2].Text = "Case Number";
                  e.Item.Cells[3].Text = "Status";
                  e.Item.Cells[4].Text = "Owner";
                  e.Item.Cells[5].Text = "Severity";
                  e.Item.Cells[6].Text = "Title";
           }

           if( (e.Item.ItemType == ListItemType.Item)
                || ( e.Item.ItemType ==
                ListItemType.AlternatingItem))
           {
                 //Fix up the age and color code it
                  DataRowView dr = (DataRowView)e.Item.DataItem;
                  int DaysOpen = Convert.ToInt32(dr[0].ToString());
                  DaysOpen = ((DaysOpen/60)/24);
                  if(DaysOpen > 30)
                       e.Item.Cells[0].BackColor = Color.Red;
                  if(DaysOpen < 30 && DaysOpen > 7)
                       e.Item.Cells[0].BackColor = Color.Yellow;
                  e.Item.Cells[0].Text = Convert.ToString(DaysOpen);
                 //Color code the severity
                  if(e.Item.Cells[5].Text.StartsWith("A"))
                        e.Item.Cells[5].BackColor = Color.Red;
                  if(e.Item.Cells[5].Text.StartsWith("B"))
                        e.Item.Cells[5].BackColor = Color.Yellow;
           }
}
End example

Team LiB
Previous Section Next Section