C# 从 Gridview 的可见 false BoundField 中检索数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11486372/
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
Retrieve data from visible false BoundField of Gridview
提问by Darshana
I have this BoundFieldin a GridView
我有这样BoundField一个GridView
<asp:BoundField DataField="ReportId" HeaderText="RId" Visible="false" />
But when I try to get text in that field, it returns empty.
但是当我尝试在该字段中获取文本时,它返回空。
protected void gvwReports_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewSchedule")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvwReports.Rows[index];
string s = row.Cells[0].Text;
}
}
but, it returns a correct value if I change BoundField's.Visibleproperty to true
但是,如果我将BoundField's.Visible属性更改为true
采纳答案by COLD TOLD
try somethink like this using client side html to hide
尝试像这样使用客户端 html 隐藏
<style type="text/css">
.hidden
{
display:none;
}
</style>
<asp:BoundField DataField="ReportId" HeaderText="RId" HeaderStyle-CssClass="hidden" >
</asp:BoundField>
回答by Waqar Janjua
According to my knoweldge when you have made the bound field invisible then you can not access it. Try using TemplateField
根据我的知识,当您使绑定字段不可见时,您将无法访问它。尝试使用 TemplateField
回答by Ceottaki
I just had the same problem.
我只是遇到了同样的问题。
Funnily enough a DataGrid won't have that problem, it will allow you to access the data from hidden columns even though it doesn't even render them in the client, because it still adds the information of the hidden columns to the ViewState.
有趣的是,DataGrid 不会有这个问题,它允许您访问隐藏列中的数据,即使它甚至没有在客户端呈现它们,因为它仍然将隐藏列的信息添加到 ViewState。
A GridView on the other hand simply ignore the hidden fields even if you set the EnableViewState property to true. The only way is to leave the information there for the client to hide with a style property, like display: none;.
另一方面,即使您将 EnableViewState 属性设置为 true ,GridView也会简单地忽略隐藏字段。唯一的方法是将信息留在那里供客户端使用样式属性隐藏,例如display: none; .
Unfortunate really, I liked the DataGrid behaviour on that, but GridView has other advantages.
不幸的是,我喜欢 DataGrid 的行为,但 GridView 有其他优点。
回答by Prashant Gupta
Although it's an year old question (in fact exactly an year old), here's another workaround without using CssClass.
尽管这是一个老问题(实际上刚好一岁),但这是另一种不使用 CssClass 的解决方法。
Just after the databind, set visibility of the desired column to false.
在数据绑定之后,将所需列的可见性设置为 false。
gridview1.databind()
gridview1.columns(i).Visibile = False
This will maintain data in viewstate but will not create markup for page.
这将在视图状态中维护数据,但不会为页面创建标记。
回答by Kermit
Seems that if a GridView column is marked as not visible it is not populated at run time so it returns nothing. So, I just populated the Hyperlink from the DataView that is bound to the Gridview remembering to declare the DataView as shared.
似乎如果 GridView 列被标记为不可见,则它不会在运行时填充,因此它不返回任何内容。所以,我只是从绑定到 Gridview 的 DataView 填充超链接,记住将 DataView 声明为共享。
I did this in VB asp.net for a GridView that finds searched events from a calendar database.
我在 VB asp.net 中为从日历数据库中查找搜索事件的 GridView 做了这个。
This worked great for me!
这对我很有用!
Private Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim ThisHyperLink As HyperLink = e.Row.Cells(0).Controls(0)
Dim drvRow As DataRowView = dvFoundEvents.Item(e.Row.DataItemIndex)
EventID = drvRow("EventID")
ThisHyperLink.NavigateUrl = "<URL>?id=" + EventID
End If
End Sub
回答by John Dunagan
This worked for me:
这对我有用:
If the column is a named DataKeyValue on your grid, you can cast the e.Item sent from the row as a DataGridItem and call its DataKeyValue. You'll need to convert it to Int, String, whatever but it will be there even if the column is visible=false.
如果列是网格上的命名 DataKeyValue,则可以将从行发送的 e.Item 转换为 DataGridItem 并调用其 DataKeyValue。您需要将其转换为 Int、String 等,但即使该列可见 = 假,它也会存在。
回答by Hugo Silva
At rowDataBound event you can access the field's value using something like:
在 rowDataBound 事件中,您可以使用以下内容访问字段的值:
(((DataRowView)e.Row.DataItem)["your_boundField_dataFieldName"]).ToString();
even if your boundfield visibility is set to false.
即使您的边界可见性设置为 false。
回答by Ruben de la Fuente
the first solution works correctly, but it was necessary add HeaderStyleto hide the header of this column
第一个解决方案工作正常,但有必要添加HeaderStyle以隐藏此列的标题
<style type="text/css">
.hidden
{
display:none;
}
</style>
<asp:BoundField DataField="ReportId" HeaderText="RId" >
<ItemStyle CssClass="hidden"/>
<HeaderStyle CssClass="hidden"/>
</asp:BoundField>

