girdView隐藏列

gridview隐藏列, 并能读取列值的解决方法。

首先 是用datakeynames 来解决

在设计数据库的时候 ,,我这里

aa

为主键。

 前台 代码

1 <asp:GridView ID="GridView1" runat="server" AllowPaging="True"  Width="798px" DataKeyNames ="aa">
2 <Columns>
3 <asp:BoundField DataField=“aa" HeaderText="车队领导姓名">
4 <HeaderStyle Font-Size="9pt" />
5
6 </asp:BoundField>

DataKeyNames 必须和

DataField的ID名字 相同。

后台 首先要获取一下 

DataKeyNames :

protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataKeyNames = new string[]{“aa”};


}

 

index 为获取行数。。。然后string ccr 来获取值。

复制代码
  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{
GridViewRow gvrow = (GridViewRow(((LinkButton)e.CommandSource).NamingContainer);

int index = gvrow.RowIndex;
string ccr = this.GridView1.DataKeys[index]["aa"].ToString ();

}
复制代码

ccr就是我们要取得隐藏列的值。

在pageload 中把这一列隐藏。我这里是第五列:

this.GridView1.Columns[5].Visible = false;

这样读出来的ccr的值就是 隐藏那个列的值。

 


方法二:

首先在前面定义CSS样式:

 <style  type="text/css">
.yangshi
{
display:none ;
}
</style>

然后在要隐藏的列调用这个样式:

<asp:BoundField DataField="guid" HeaderText="guid" >
<!--调用前面定义的样式,隐藏这一列--!>
<HeaderStyle CssClass="yangshi" />
<ItemStyle CssClass="yangshi" />
</asp:BoundField>

这样,在后台就能直接取到隐藏这一列的guid值。  string guid = GridView1.Rows[0].Cells[4].Text;


方法三:

这个是在gridview中加上  onrowcreated  事件,在这个事件中隐藏列(我需要隐藏的是GUID的列)。

复制代码
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcreated="GridView1_RowCreated" 
>
<Columns>
<asp:BoundField DataField="zcbm" HeaderText="资产编码" />
<asp:BoundField DataField="zcmc" HeaderText="资产名称" />
<asp:BoundField DataField="ggxh" HeaderText="规格型号" />
<asp:BoundField DataField="jszk" HeaderText="技术状况" />
<asp:BoundField DataField="guid" HeaderText="guid" >
</asp:BoundField>
</Columns>
</asp:GridView>
复制代码

 onrowcreated  事件:

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[4].Visible = false;//guid列的隐藏
}

直接取隐藏列guid的值。 string guid = GridView1.Rows[0].Cells[4].Text;

原文地址:https://www.cnblogs.com/ljj4490/p/5787033.html