Colorize Rows in DataGrid other than alternating

M

MattC

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; }
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top