我如何从gridview中的隐形单元格中获取id?

时间:2012-11-16 03:33:36

标签: c# asp.net gridview

我在gridview中做了隐形的第一列。:

<asp:GridView ID="tableResults" runat="server" DataMember="Table"
    EnableModelValidation="True" CssClass = "GridViewStyle"
    AutoGenerateColumns="False" OnRowDataBound = "tableResults_DataBound"
    OnRowDeleting = "tableResults_RowDeleting" AutoGenerateDeleteButton="True">                     
   <HeaderStyle CssClass = "GridViewHeaderStyle" />
   <RowStyle CssClass = "GridViewRowStyle"/>    
   <Columns>                   
    <asp:BoundField DataField="I_ID" Visible="false" HeaderText=""/>
    <asp:BoundField DataField="I_MAJOR" HeaderText="Major"/>
    <asp:BoundField DataField="I_MINOR" HeaderText="Minor"/>
    <asp:BoundField DataField="I_RELEASE" HeaderText="Release"/>
    <asp:BoundField DataField="I_BUILD" HeaderText="Build"/>
   </Columns>           
</asp:GridView>

现在当我从第1列获得值时,它为null:

TableCell rowData = tableResults.Rows[e.RowIndex].Cells[1];
oracleCom.CommandText = "Delete From TBL_VERSIONS Where i_id = " + rowData.Text;

如果我做了专栏visible = true,我就能获得价值。但是这个专栏必须是不可见的。

2 个答案:

答案 0 :(得分:1)

将DataKeyNames设置为I_ID

<asp:GridView ID="tableResults" runat="server" DataMember="Table"
    EnableModelValidation="True" CssClass = "GridViewStyle"
    AutoGenerateColumns="False" OnRowDataBound = "tableResults_DataBound"
    OnRowDeleting = "tableResults_RowDeleting" AutoGenerateDeleteButton="True"
    DataKeyNames = "I_ID" >

然后你可以获得如下ID

 int idVal= (int)tableResults.DataKeys[e.RowIndex].Value;

或者您可以在gridview行数据绑定事件上设置单元格不可见,删除DataField上的不可见属性=&#34; I_ID&#34;。

protected void tableResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
   e.Row.Cells[1].Visible = false;
}

答案 1 :(得分:0)

一个简单的方法:

保持列可见

columnName.visible = true

但是使用HTML

使其不可见
style="visibility:hidden;"

OR

style="display:none;"

即喜欢

<asp:BoundField DataField="I_ID" Visible="false" style="visibility:hidden;" HeaderText=""/>

通过这种方式,您可以隐藏它并访问其值。