none
Set row ForeColor property - InvalidCastException was unhandled by user code

    Question

  •  

    Hi All,

    I am using a GridView to display some rows. Depending on a certain conditions, I wish to set the row colour to red or white.

     

    The gridview is bound to a ObjectDataSource, and I have put the check for the condition into the SQL for the selectmethod belonging to the datasource and added an extra column ('HasWorkItem') which can be red or white.

     

    There is a couple of approaches I am thinking about but I am hitting dead ends on all of them. I think option 3 is the most viable option but if anyone can give some tips on the other approaches or the best option etc.

     

    1. Set the Rowstyle

    <RowStyle ForeColor= '<%# Eval("HasWorkItem") %>' />   gives me an error ....

    Databinding expressions are only supported on objects that have a DataBinding event.

     

    2. Set ForeColor per column

    <asp:TemplateField HeaderText="Work Type" ItemStyle-Wrap="false" HeaderStyle-Wrap="false" >

    <ItemTemplate>

    <asp:LinkButton ID="AssignTaskLinkButton" runat="server" ForeColor='<%# Eval("HasWorkItem") %>' CommandArgument='<%# Eval("OrderLineWorkItemId") %>' OnClick="AssignTaskLinkButton_Click"><%# GetWorkTypeById((int)Eval("TaskItemTypeId")) %></asp:LinkButton>

    </ItemTemplate>

    </asp:TemplateField>    gives me an error ...

    InvalidCast Exception was unhandled by user code

     

    3. Associated a method with the RowCreated event

    okay I can access

     

    e.Row.ForeColor =

     

    but my problem here is that I don't know how to get to the column 'HasWorkItem' in the corresponding row in the datasource.

     

    Can you please help me to get option 3 off the ground. Or a way of doing it in the .aspx

    Friday, October 19, 2007 9:25 AM

Answers

  • Well its done. Crikey ... all I wanted to do was set some row text a different colour. I've lost an eternity faffing about with it. But maybe this can help someone else.

     

    I couldn't figure out how to set the whole row colour. In the end I went for the cell by cell option.

     

    Code Block

    protected void PendingTaskItems_RowDataBound(object sender, GridViewRowEventArgs e)

    {

    if (e.Row.RowType == DataControlRowType.DataRow)

    {

    System.Drawing.ColorConverter colConv = new System.Drawing.ColorConverter();

    DataRowView rowView = (DataRowView)e.Row.DataItem;

    LinkButton assignTaskBtn = (LinkButton)e.Row.FindControl("AssignTaskLinkButton");

    assignTaskBtn.ForeColor = (System.Drawing.Color)colConv.ConvertFromString(rowView["HasWorkItem"].ToString());

    for(int i=0; i <= e.Row.Cells.Count -1; i++)

    {

    e.Row.Cells[i].ForeColor = (System.Drawing.Color)colConv.ConvertFromString(rowView["HasWorkItem"].ToString());

    }

    }

    }

     

     

     

    Friday, October 19, 2007 2:23 PM

All replies

  •  

    Okay went with 3 and came up with

     

    protected void PendingTaskItems_RowDataBound(object sender, GridViewRowEventArgs e)

    {

    if (e.Row.RowType == DataControlRowType.DataRow)

    {

    DataRowView rowView = (DataRowView)e.Row.DataItem;

    //e.Row.Style["background-color"] = rowView["HasWorkItem"].ToString();

    e.Row.Style["color"] = rowView["HasWorkItem"].ToString();  //setting font colour

    }

    }

     

    so my problem of accessing the value is fixed.

    However trying to set the text colour is not working.

    I can set the background colour but this isn't really what I wanted.

     

    Anyone know why?

     

     

     

    Friday, October 19, 2007 11:52 AM
  • Well its done. Crikey ... all I wanted to do was set some row text a different colour. I've lost an eternity faffing about with it. But maybe this can help someone else.

     

    I couldn't figure out how to set the whole row colour. In the end I went for the cell by cell option.

     

    Code Block

    protected void PendingTaskItems_RowDataBound(object sender, GridViewRowEventArgs e)

    {

    if (e.Row.RowType == DataControlRowType.DataRow)

    {

    System.Drawing.ColorConverter colConv = new System.Drawing.ColorConverter();

    DataRowView rowView = (DataRowView)e.Row.DataItem;

    LinkButton assignTaskBtn = (LinkButton)e.Row.FindControl("AssignTaskLinkButton");

    assignTaskBtn.ForeColor = (System.Drawing.Color)colConv.ConvertFromString(rowView["HasWorkItem"].ToString());

    for(int i=0; i <= e.Row.Cells.Count -1; i++)

    {

    e.Row.Cells[i].ForeColor = (System.Drawing.Color)colConv.ConvertFromString(rowView["HasWorkItem"].ToString());

    }

    }

    }

     

     

     

    Friday, October 19, 2007 2:23 PM
  • Try this:

     

    protected void PendingTaskItems_RowDataBound(object sender, GridViewRowEventArgs e)

    {

    DataRowView drv = (DataRowView)e.Row.DataItem; 
    if (drv["ColumnName"] == someValue)
    {

    e.Row.ForeColor = System.Drawing.Color.Blue;

    }

    }

     

     

    Thursday, May 01, 2008 4:27 PM