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

Help with a datagrid

Status
Not open for further replies.

sthmpsn1

MIS
Sep 26, 2001
456
US
I have a datagrid that I want to color the text of each row based on the value in code <%# Container.DataItem( &quot;code&quot; ) %>. Depending on the value in that field I want to rows forecolor to be a different color. How would I do this. Any help would be great!

<asp:datagrid id=&quot;themonthdata&quot; runat=&quot;server&quot; Width=&quot;100%&quot; BorderStyle=&quot;Solid&quot; CellPadding=&quot;2&quot;
AutoGenerateColumns=&quot;False&quot; CssClass=&quot;maintexty&quot; AllowSorting=&quot;True&quot; DataKeyField=&quot;ID&quot; OnItemDataBound=&quot;myGrid_ItemDataBound&quot;
BorderColor=&quot;Black&quot; onEditCommand=&quot;DataGrid_Edit&quot; onCancelCommand=&quot;DataGrid_Cancel&quot; onUpdateCommand=&quot;DataGrid_Update&quot;
OnItemCreated=&quot;DataGrid_ItemCreated&quot; OnDeleteCommand=&quot;DataGrid_Delete&quot; onSortCommand=&quot;DataGrid_Sort&quot;>
<columns>
<asp:TemplateColumn>
<itemtemplate>
<asp:ImageButton CommandName=&quot;Delete&quot; ID=&quot;images&quot; runat=&quot;server&quot; ImageUrl=&quot;images/icon-delete.gif&quot; />
</itemtemplate>
</asp:TemplateColumn>

<asp:editcommandcolumn ButtonType=&quot;LinkButton&quot; CancelText=&quot;Cancel&quot; editText=&quot;Edit&quot; UpdateText=&quot;Update&quot; />
<asp:boundcolumn DataField=&quot;ID&quot; HeaderText=&quot;ID&quot; ReadOnly=&quot;true&quot; Visible=&quot;False&quot; ItemStyle-CssClass=&quot;maintexty&quot; SortExpression=&quot;ID&quot; />
<asp:boundcolumn DataField=&quot;loginID&quot; HeaderText=&quot;Login ID&quot; ReadOnly=&quot;true&quot; ItemStyle-CssClass=&quot;maintexty&quot; SortExpression=&quot;loginID&quot; />
<asp:boundcolumn DataField=&quot;month&quot; HeaderText=&quot;Month&quot; ReadOnly=&quot;true&quot; ItemStyle-CssClass=&quot;maintexty&quot; SortExpression=&quot;month&quot; />
<asp:TemplateColumn><headertemplate>Date</headertemplate><itemtemplate><%# Container.DataItem( &quot;date&quot; ) %></itemtemplate>
<edititemtemplate>
<asp:TextBox ID=&quot;thedategrid&quot; Text='<%# Container.DataItem( &quot;date&quot; ) %>' runat=&quot;server&quot; />
<asp:RequiredFieldValidator ControlToValidate=&quot;thedategrid&quot; Display=&quot;Dynamic&quot; Text=&quot;Required!&quot; runat=&quot;server&quot;/>
<asp:CompareValidator ControlToValidate=&quot;thedategrid&quot; Display=&quot;Dynamic&quot; Text=&quot;Must Be A Date!&quot; Operator=&quot;DataTypeCheck&quot; Type=&quot;Date&quot; runat=&quot;server&quot;/>
</edititemtemplate>
</asp:TemplateColumn>

<asp:TemplateColumn><headertemplate>Hours</headertemplate><itemtemplate><%# Container.DataItem( &quot;hour&quot; ) %></itemtemplate>
<edititemtemplate>
<asp:TextBox ID=&quot;thehourgrid&quot; Text='<%# Container.DataItem( &quot;hour&quot; ) %>' runat=&quot;server&quot; />
<asp:RequiredFieldValidator ControlToValidate=&quot;thehourgrid&quot; Display=&quot;Dynamic&quot; Text=&quot;Required!&quot; runat=&quot;server&quot;/>
<asp:CompareValidator ControlToValidate=&quot;thehourgrid&quot; Display=&quot;Dynamic&quot; Text=&quot;Must Be A Number!&quot; Operator=&quot;DataTypeCheck&quot; Type=&quot;Double&quot; runat=&quot;server&quot;/>
</edititemtemplate>
</asp:TemplateColumn>

<asp:TemplateColumn><headertemplate>Code</headertemplate><itemtemplate><%# Container.DataItem( &quot;code&quot; ) %></itemtemplate>
<edititemtemplate>
<asp:TextBox ID=&quot;thecodegrid&quot; Text='<%# Container.DataItem( &quot;code&quot; ) %>' runat=&quot;server&quot; />
<asp:RequiredFieldValidator ControlToValidate=&quot;thecodegrid&quot; Display=&quot;Dynamic&quot; Text=&quot;Required!&quot; runat=&quot;server&quot;/>
<asp:CompareValidator ControlToValidate=&quot;thecodegrid&quot; Display=&quot;Dynamic&quot; Text=&quot;Must Be A Letter!&quot; Operator=&quot;DataTypeCheck&quot; Type=&quot;String&quot; runat=&quot;server&quot;/>
</edititemtemplate>
</asp:TemplateColumn>
<asp:boundcolumn DataField=&quot;approval&quot; HeaderText=&quot;Is Approved?&quot; SortExpression=&quot;approval&quot; />
</columns>
</asp:datagrid>
 
You could check the value of the cell that you are concerned with and change the forecolor based on that. For example:

With Datagrid1
For I = 0 to (.Items.Count - 1)
If .Item(I).Cell(CellYouWanttoCheck) = ValueYouWantToCheckFor Then
.Forecolor = ColorofChoice
Else
.Forecolor = OtherColor
End If
Next I
End With

I haven't actually tested this bit of code so there might be syntax errors, but this works in principle.

Eva
 
I tried this and didn't get any errors but also didn't get the color change.

With themonthdata
For i = 0 To (.Items.Count - 1)
If themonthdata.Items(i).Cells(7).Text = &quot;v&quot; Then
objColor = Color.Blue
.ForeColor = objColor
End If
Next i
End With



 
In the ItemDataBound event handler of your grid put this code(C# sample, please convert):
Code:
private void themonthdata_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
  if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
  {	
    if(e.Item.Cells[7].Text == &quot;v&quot;)
    {
      e.Item.BackColor = Color.Blue;
    }
    else
    {
       e.Item.BackColor = Color.White;
    }
  }
}

private void InitializeComponent()
{
  this.themonthdata.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.themonthdata_ItemDataBound);
}
 
Woopss... sorry, told you I hadn't really tested it.

Try
With themonthdata
For i = 0 To (.Items.Count - 1)
If .Items(i).Cells(7).Text = &quot;v&quot; Then
objColor = Color.Blue
.Items(i).Cells(7).ForeColor = objColor
End If
Next i
End With

That should do what you need now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top