Gridview Boundfield问题

时间:2011-12-15 19:46:57

标签: c# asp.net gridview

我的表格上有一些带有一些字段的网格。我在gridview中添加了两个字段,因为我想在后面的代码中使用它们中的数据,我在后面的代码中获取它们,但事情是我不希望这些列在gridview中可见,所以我试图设置他们的可见属性'False'但是没有用,我没有访问他们的数据。怎么能实现?

        <asp:BoundField DataField="Service_Id" HeaderText="Service_Id"   SortExpression="Service_Id" HeaderStyle-BackColor="Gray"
            Visible="true">
            <HeaderStyle BackColor="Gray"></HeaderStyle>
        </asp:BoundField>
        <asp:BoundField DataField="UserId" HeaderText="UserID" SortExpression="UserId" HeaderStyle-BackColor="Gray"
            Visible="true">
            <HeaderStyle BackColor="Gray"></HeaderStyle>
        </asp:BoundField>

这是我的页面背后的代码:

    Button Button1 = (Button)sender;
        GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent;
        HiddenFieldServiceID.Value = grdRow.Cells[0].Text;
        HiddenFieldUserID.Value = grdRow.Cells[1].Text;

1 个答案:

答案 0 :(得分:5)

您不应该使用BoundField。请改用DataKeyNames属性。 然后可以使用DataKeys[rowIndex]

获取值

ASPX:

DataKeyNames="Service_Id, UserId";

代码:

var Service_Id = (int)gv.DataKeys[rowIndex]["Service_Id"];
var UserId = (int)gv.DataKeys[rowIndex]["UserId"];
相关问题