I have a large gridview:
<asp:GridView CssClass="hoursGrid" ID="hoursReportGridView" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataSourceID="SqlDataSource2" OnRowDataBound="hoursReportGridView_OnRowDataBound">
<Columns>
<asp:BoundField DataField="Person" HeaderText="Person" SortExpression="Project" />
<asp:BoundField DataField="Project" HeaderText="Project" SortExpression="Project" />
<asp:BoundField DataField="ProjectType" HeaderText="Project Type" ReadOnly="True" SortExpression="Sprint" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="StoryNumber" HeaderText="Story Number" SortExpression="Story" ItemStyle-Width="6%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="StoryTitle" HeaderText="Story Title" SortExpression="Story" ItemStyle-Width="20%" />
<asp:BoundField DataField="Effort" HeaderText="Effort" SortExpression="Effort" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Task" HeaderText="Task" SortExpression="Task" ItemStyle-Width="20%" />
<asp:BoundField DataField="OriginalEstimateHours" HeaderText="Original Estimate" SortExpression="OriginalEstimateHours" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Monday" HeaderText="Monday" ReadOnly="True" SortExpression="Monday" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Tuesday" HeaderText="Tuesday" ReadOnly="True" SortExpression="Tuesday" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Wednesday" HeaderText="Wednesday" ReadOnly="True" SortExpression="Wednesday" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Thursday" HeaderText="Thursday" ReadOnly="True" SortExpression="Thursday" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Friday" HeaderText="Friday" ReadOnly="True" SortExpression="Friday" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Saturday" HeaderText="Saturday" ReadOnly="True" SortExpression="Saturday" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="Sunday" HeaderText="Sunday" ReadOnly="True" SortExpression="Sunday" ItemStyle-HorizontalAlign="Right" />
<asp:TemplateField HeaderText="Total" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:LinkButton ID="taskLinkButton" Text='<%# Eval("Total") %>' Visible='<%# Eval("StoryTitle").ToString() != "" %>' runat="server" OnClick="taskLinkButton_Click" />
<asp:Literal ID="Literal1" Text='<%# Eval("Total") %>' Visible='<%# Eval("StoryTitle") == "" %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DifferentUsers" HeaderText="DifferentUsers" SortExpression="DifferentUsers" Visible="false"/>
</Columns>
</asp:GridView>
The last boundfield I do not want to show to the user, which is why its visibility is false.
However, I want to change the color of the cell if this invisible cell for the row is > 0:
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.Cells[16].Text != " ") && (Int16.Parse(e.Row.Cells[16].Text) > 0))
{
for (int i = 0; i < 15; i++)
{
e.Row.Cells[i].ForeColor = Color.Black;
e.Row.Cells[i].BackColor = ColorTranslator.FromHtml("#fde16d");
}
}
}
}
THis method works fine with the column is visible, but it does not work when I set it to false. How do I achieved functionality without showing the column?
Instead of hiding that cell, use a TemplateField
that contains an ASP.NET HiddenField
control, like this:
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="HiddenFieldDifferentUsers" Value='<%# Eval("DifferentUsers") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
Now in your code-behind, you can find the hidden field control, like this:
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField theHiddenField = e.Row.FindControl("HiddenFieldDifferentUsers") as HiddenField;
// Check that we successfully found hidden field before using it
if(theHiddenField != null)
{
// Do something with hidden field here if you need to
}
}
}
Try this
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
DataRowView rowView = (DataRowView)e.Row.DataItem;
int a = Convert.ToInt32(rowView["DifferentUsers"]);
if(a>0)
{
for (int i = 0; i < 15; i++)
{
e.Row.Cells[i].ForeColor = Color.Black;
e.Row.Cells[i].BackColor = ColorTranslator.FromHtml("#fde16d");
}
}
}
reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With