在后面的代码中获取隐藏的列值 [英] Get hidden column value in code behind

查看:90
本文介绍了在后面的代码中获取隐藏的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有第一列被隐藏的gridview.但是,我想在代码中获取其价值.以下是我正在尝试的代码:

Hello,

I have gridview in which 1st column is hidden. However I want to get its value in code behind. Following is the code I am trying:

<asp:GridView ID="gvShowRequests" runat="server" AllowPaging="true" PageSize="15"
 AutoGenerateColumns="false" Width="60%" AllowSorting="false" GridLines="Both"
 BorderColor="Black" PagerStyle-CssClass="pager" OnPageIndexChanging="gvShowRequests_PageIndexChanging"                                     BorderStyle="Groove" OnRowDataBound="gvShowRequests_RowDataBound" OnRowEditing="gvShowRequests_RowEditing" OnRowUpdating="gvShowRequests_RowUpdating">
<RowStyle CssClass="gvRows" />
<AlternatingRowStyle CssClass="gvRowsalt" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" Visible="false" />
<asp:BoundField DataField="Name" HeaderText="Application" />
<asp:BoundField DataField="CostCentre" HeaderText="Cost Centre" />
<asp:BoundField DataField="UserName" HeaderText="User Name" />
<asp:BoundField DataField="Type" HeaderText="Request Type" />
</Columns>
</asp:GridView>

和后面的代码:

And code behind:

protected void gvShowRequests_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DropDownList gvddl = gvShowRequests.Rows[e.RowIndex].FindControl("gvddlRequestStatus") as DropDownList;

            RequestStatus? updatedRequestStatus = this.GetSelectedRequestStatus(gvddl);
            
            int rowIndex=gvShowRequests.EditIndex;
            int Id = (int)gvShowRequests.DataKeys[rowIndex].Value;
            new RequestStore().UpdateRequest(Id, updatedRequestStatus);

        }


运行代码时,出现错误,因为索引超出范围.必须为非负数并且小于集合的大小.
参数名称:"int Id = (int)gvShowRequests.DataKeys[rowIndex].Value;"
行上的索引"
我的代码有什么问题?


When I am running the code I am getting error as "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index" on row "int Id = (int)gvShowRequests.DataKeys[rowIndex].Value;"

What is wrong with my code?

推荐答案

您没有在Gridview源代码中提及数据键名,因此在gridview中提及数据键名,例如,

You did''t mention datakeynames in Gridview source code,So Mention datakeynames in gridview like,

DataKeyNames="Id"



现在可以通过以下方式获取不可见列的值:



and now get the value of the invisible column in following way:

Int16 value=Convert.ToInt16(gv_gvShowRequests.DataKeys[RowIndex].Value);



其中RowIndex是要获取其ID的行的索引.



Where RowIndex is the index of row of which you want to get the ID.


这是错误的方法.数据键并不意味着您会获得第一列. DataKey与GridView的"DataKeyNames"属性相关.您需要在gridview中分配数据键名,例如< asp:gridview datakeynames ="value" xmlns:asp =#unknown"/>.这里的值应该是您在第一栏中使用的ID.

有关更多参考,请参见下面的链接,

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx

http://www.codeproject.com/KB/grid/Data_presentation.aspx
This is wrong method. Datakey does not mean you will get at first column. DataKeys are related to "DataKeyNames" Property of GridView. You need to Assign datakeynames in gridview like "<asp:gridview datakeynames="value" xmlns:asp="#unknown" />". Here value should be your Id, which you have used in your first column.

For More reference see below links,

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx

http://www.codeproject.com/KB/grid/Data_presentation.aspx


这篇关于在后面的代码中获取隐藏的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆