locked
Format a cell during rowdatabound in a gridview RRS feed

  • Question

  • User-1420496881 posted

    I'm attempting to change the format of the 2nd column in a gridview during the rowdatabound event.  I pulled sample code from various sources and wrote the following, but it's not working.  I tried to do a trace, but the rowdatabound event didn't appear in the trace.  Here's some code for the gridview:


            <asp:GridView ID="gvCategorySummary" runat="server" 
                DataSourceID="odsCategorySummary" AllowPaging="False" AllowSorting="true"
                AutoGenerateColumns="False" OnRowCommand="gvCategorySummary_RowCommand" OnRowDataBound="gvCategorySummary_RowDataBound">
                <Columns>
                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:Button ID="btnShowEntries" runat="server" CommandName="ShowEntries" Text="Show Entries"
                                CommandArgument='<%# Bind("CategoryID") %>'></asp:Button>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-HorizontalAlign="Right">
                        <HeaderTemplate>Year</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblYear" runat="server" Text='<%# Bind("Year") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView
    


    Here's what I have in the code behind:


            protected void gvCategorySummary_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
                }
            }
    

    I expected the values in the 2nd template column to get written in italics, but nothing happens.  I do have a gridview skin that's getting applied.

    Thursday, August 12, 2010 10:25 PM

Answers

  • User-653327211 posted

    a skin will override the gridview's styles however you can set your cssclass inside the gridview tag equal to ""

    try this out:

    e.Row.Cells(1).Font.Italic = True


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 12, 2010 10:50 PM

All replies

  • User-1734649046 posted

    Hi,

    If you just want to format Label control, you can set Font-Italic property to true. Please change your lblYear declaration as follows

    <asp:Label ID="lblYear" runat="server" Text='<%# Bind("Year") %>' Font-Italic="True"></asp:Label>

    hope this helps

    Thursday, August 12, 2010 10:48 PM
  • User-653327211 posted

    a skin will override the gridview's styles however you can set your cssclass inside the gridview tag equal to ""

    try this out:

    e.Row.Cells(1).Font.Italic = True


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 12, 2010 10:50 PM
  • User-1420496881 posted

    The example posted isn't the end game.  The actual gridview has 8 columns, the last of which is a numeric value.  If the value is >0, I want to change the font color to green, if it's less than 0, I want to change the font color to red.  The posted example was just meant to make sure I'm taking the right approach.

    I've also tried removing the gridview skin, but it did not help.

    Thursday, August 12, 2010 10:53 PM
  • User-818134166 posted

    Try this:

    e.Row.Cells[1].CssClass = "Class1";

    You can declare the cssclas as

    .Class1
    {
       font-style:italic;
    }

    .Class1

    {

       font-style:italic;

    }


    --dmnida
    *Mark as Answered if it helped


    Thursday, August 12, 2010 10:54 PM
  • User-1420496881 posted

    This actually works for my example.  Thanks.

    Thursday, August 12, 2010 10:56 PM
  • User-818134166 posted

    Try something like this for color change or a css:

            if (e.Row.Cells[0].Text == "njain Active")
            {
                e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
            }


            if (e.Row.Cells[0].Text == "1")

            {

                e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;

            }

    --dmnida


    Thursday, August 12, 2010 10:59 PM
  • User-1734649046 posted

    Hi,

    Please refer following

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (Convert.ToInt16(e.Row.Cells[1].Text) <= 0)
        {
            e.Row.Cells[1].BackColor = Color.Red;
        }
        else if (Convert.ToInt16(e.Row.Cells[1].Text) > 0)
        {
            e.Row.Cells[1].BackColor = Color.Green;
        }
    }

    hope this helps

    Thursday, August 12, 2010 11:04 PM
  • User-1420496881 posted

    Ketan,

    Thank you for that example.  There's one additional wrinkle.  The value in Cells[1] is formatted to currency in the gridview.  Therefore, when I try to convert it to an integer, I get an error.  I found the following code that converts a currency string back to an integer, but it is not working:

    int difference = int.Parse(e.Row.Cells[1].Text, NumberStyles.Currency)

    I get an 'Input string was not in correct format' error.  I used the {0:c0} format for currency, so I have commas and parentheses for negative values in addition to the actual dollar sign.  I could do a string replace for those 4 characters, but it seems like there's a better way.


    Matt


    Friday, August 13, 2010 8:41 AM
  • User-1420496881 posted

    I figured it out.  The Cells[1].Text method was returning an empty string.  Here's the full code that now works:


            protected void gvCategorySummary_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    int difference = int.Parse(((Label)e.Row.Cells[7].Controls[1]).Text, NumberStyles.Currency);
    
                    if (difference < 0)
                    {
                        e.Row.Cells[7].CssClass = "baddifference";
                    }
                    else if (difference > 0)
                    {
                        e.Row.Cells[7].CssClass = "gooddifference";
                    }
    
                }
            }
    


    The only part of the code I don't fully understand is the controls[1] part.  I'm not sure what the controls collection includes.


    Matt


    Friday, August 13, 2010 9:18 AM