views:

1286

answers:

3

Is it possible to give individual cells in a data grid view row different styles such as backcolor, fontcolor etc?

I do not mean giving the whole row a new style, only a specific cell.

A: 

Something like this?

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
      string imageName = e.Row.Cells[0].Text.ToString();
      e.Row.Cells[1].Attributes.Add(“Style”,
        “background-image: url(’images/” + imageName + “‘);”);    
  }
}
Gustavo
post is tagged as windows forms, vb.net
Maslow
A: 

In VB example. The key is to access the RowDataBound event and use the e.Row.Cells(number).Attributes.Add function.

Example below shows how you could add a mouse over event to effect the cell.

Protected Sub gridview1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridview1.RowDataBound

    ' If it got past the header   
    If e.Row.RowType = DataControlRowType.DataRow Then

        ' When mouse over occurs      
        e.Row.Cells(1).Attributes.Add("onMouseOver", "this.style.fontWeight='Bold'")

    End If

End Sub
kevchadders
The asker wanted to style individual cells in a row, not the entire row.
Cory Larson
Doh! Me bad... thats what i get for rushing before lunch. I've edited it.
kevchadders
+2  A: 

certainly:

Me.myDatagridview.Rows(0).Cells(0).Style.ForeColor = Color.Aqua

Maslow