Reputation: 7532
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?
Upvotes: 5
Views: 28619
Reputation: 426
Using VB rather than C#, I was just able to use this:
Dim myWordList As String = e.Row.DataItem("myWordList").ToString
i.e. using e.Row.DataItem("DataFieldName") rather than e.Row.FindControl("DataFieldName").
To retrieve the value of hidden bound field:
<asp:BoundField DataField="myWordList" HeaderText="myWordList"
SortExpression="BadWordList" Visible="False" />
I believe this is the equivalent C#:
string myWordList = e.Row.DataItem("myWordList").ToString;
It works for me. ;) In fact, it appears that the bound column need not even be in the gridview, as long as the datafield is in the returned recordset.
Upvotes: 1
Reputation: 1
Try this Friends ,
I took a table Branch with colums BranchID & Branchname to hide BranchID I took HiddenField inside TemplateField as shown below
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="BranchID" Value='<%# Eval("BranchID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
In aspx.cs
HiddenField hd = row.FindControl("BranchID") as HiddenField;
string str = "UPDATE Branch set BranchName='" + obj.BranchName + "' where BranchId = " + hd.Value + " ";
Upvotes: 0
Reputation: 599
Depending on how you are binding to the GridView
, the row's DataItem
can be cast to your data class:
protected void hoursReportGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
MyData myData = e.Row.DataItem as myData;
if (myData != null && myData.DifferentUsers > 0)
{
e.Row.ForeColor = Color.Black;
e.Row.BackColor = ColorTranslator.FromHtml("#fde16d");
}
}
This provides IntelliSense at development time, and strongly typed checking at compile time.
Upvotes: 0
Reputation: 4354
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");
}
}
}
Upvotes: 3
Reputation: 34846
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
}
}
}
Upvotes: 10
Reputation: 2367
You could try and see if setting the visibility to true before changing the color and setting back to false after that works. The user will never see the column, since the grid will not get refreshed during the operation.
Upvotes: 1