Colorize Rows in DataGrid other than alternating

Discussion in 'ASP .Net' started by MattC, Mar 22, 2005.

  1. MattC

    MattC Guest

    Hi,

    I have place the following code in my ItemDataBound event:

    The problem I have is that when I select a row for editing using the edit
    command it no longer changes the CSSClass to
    the one specified in SelectedItemStyle.

    TIA

    MattC

    private void _timesheetentriesgrid_ItemDataBound(object sender,
    DataGridItemEventArgs e)
    {

    if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==
    ListItemType.Item )
    {
    ImageButton ib = (ImageButton)e.Item.FindControl("_tdel");
    ib.Attributes.Add("onclick","return confirm('Are you sure you wish to
    delete?');");

    string thisday =
    ((TimesheetActivityEntry)e.Item.DataItem).EntryDate.DayOfWeek.ToString();

    //test to colorise by day
    if(thisday != CurrentDay)
    {
    CurrentDay = thisday;
    Altenator = !Altenator;
    }

    if(Altenator == true)
    e.Item.CssClass = "gridline";
    else
    e.Item.CssClass = "gridlinealt";
    }

    My DataGrid it declared:

    <asp:DataGrid id="_tgrid" CssClass="grid" Runat="server"
    AutoGenerateColumns="False" ShowFooter="True">
    <AlternatingItemStyle
    CssClass="gridlinealt"></AlternatingItemStyle>
    <ItemStyle CssClass="gridline"></ItemStyle>
    <HeaderStyle CssClass="gridcell"></HeaderStyle>
    <FooterStyle CssClass="gridfooter"></FooterStyle>
    <SelectedItemStyle CssClass="gridselected"></SelectedItemStyle>
    <Columns>
    <asp:TemplateColumn HeaderText="Day">
    <ItemTemplate>
    <%# DataBinder.Eval(Container, "DataItem.EntryDate.DayOfWeek")
    %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Project">
    <ItemTemplate>
    <%# ProjectStatus(DataBinder.Eval(Container,
    "DataItem.EntryProject")) %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Call Number">
    <ItemTemplate>
    <%# DataBinder.Eval(Container, "DataItem.CallNumber") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Activity">
    <ItemTemplate>
    <%# DataBinder.Eval(Container, "DataItem.Activity.ActivityName")
    %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Hours">
    <ItemTemplate>
    <%# Hours(DataBinder.Eval(Container, "DataItem.Hours")) %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Comments">
    <HeaderStyle CssClass="comments"></HeaderStyle>
    <ItemStyle HorizontalAlign="Left"></ItemStyle>
    <ItemTemplate>
    <%# DataBinder.Eval(Container, "DataItem.Comments") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Edit">
    <HeaderStyle CssClass="editcell"></HeaderStyle>
    <ItemTemplate>
    <asp:imagebutton runat="server" ImageUrl="../images/edit.gif"
    AlternateText="Edit" CommandName="Edit"
    CausesValidation="false" ID="_tedit"></asp:imagebutton>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Delete">
    <HeaderStyle CssClass="editcell"></HeaderStyle>
    <ItemTemplate>
    <asp:imagebutton Runat="server" ImageUrl="../images/delete.gif"
    AlternateText="Delete" CommandName="Delete"
    CausesValidation="False" ID="_tdel"></asp:imagebutton>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>



    ViewState properties:

    public string CurrentDay
    {
    get
    {
    if(ViewState["entryday"] == null)
    ViewState["entryday"] = String.Empty;

    return ViewState["entryday"].ToString();
    }
    set{ ViewState["entryday"] = value; }
    }

    public bool Altenator
    {
    get
    {
    if(ViewState["onoff"] == null)
    ViewState["onoff"] = false;

    return Convert.ToBoolean(ViewState["onoff"]);
    }
    set{ ViewState["onoff"] = value; }
    }
     
    MattC, Mar 22, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stuart Shay
    Replies:
    3
    Views:
    13,307
    Darkmoor
    Aug 20, 2010
  2. Replies:
    3
    Views:
    645
  3. Sachin
    Replies:
    1
    Views:
    391
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jun 1, 2007
  4. MangroveRoot

    alternating text/image rows

    MangroveRoot, Nov 4, 2007, in forum: HTML
    Replies:
    7
    Views:
    446
    Adrienne Boswell
    Nov 4, 2007
  5. Replies:
    4
    Views:
    208
Loading...

Share This Page