C# 从隐藏列获取单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19793050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting cell value from hidden column
提问by David Tunnell
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:
但是,如果该行的这个不可见单元格 > 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?
此方法在列可见时工作正常,但当我将其设置为 false 时它不起作用。如何在不显示列的情况下实现功能?
采纳答案by Karl Anderson
Instead of hiding that cell, use a TemplateField
that contains an ASP.NET HiddenField
control, like this:
不要隐藏该单元格,而是使用TemplateField
包含 ASP.NETHiddenField
控件的 ,如下所示:
<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
}
}
}
回答by ThunderGr
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.
您可以尝试在更改颜色之前将可见性设置为 true 并在此之后设置回 false 。用户永远不会看到该列,因为在操作期间网格不会刷新。
回答by Sid M
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");
}
}
}
回答by Mark
Depending on how you are binding to the GridView
, the row's DataItem
can be cast to your data class:
根据您绑定到 的方式GridView
,DataItem
可以将行转换为您的数据类:
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.
这在开发时提供 IntelliSense,并在编译时提供强类型检查。
回答by Supriya
Try this Friends ,
试试这个朋友,
I took a table Branch with colums BranchID & Branchname to hide BranchID I took HiddenField inside TemplateField as shown below
我拿了一个带有列 BranchID 和 Branchname 的表格 Branch 来隐藏 BranchID 我把 HiddenField 放在 TemplateField 中,如下所示
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="BranchID" Value='<%# Eval("BranchID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
In aspx.cs
在 aspx.cs
HiddenField hd = row.FindControl("BranchID") as HiddenField;
string str = "UPDATE Branch set BranchName='" + obj.BranchName + "' where BranchId = " + hd.Value + " ";